float value mapped to unsigned char

hi,

im trying to convert a decimal value from 0-1 to unsigned char. so since there are 256 value of unsigned char. how can i do that? i mean since you can only define unsigned char with 'a' or '@' which is not a math value, how can i convert those 0-1 decimal to unsigned char character?

thanks in advance
1
2
float f = /*...*/;
unsigned char c = f;//this works 
What you are looking for is called type casting, there's a complete tutorial page dedicated to it that explains implicit conversion (like hamsterman's), the more proper explicit conversion and finally some "advanced" casting functions:
http://www.cplusplus.com/doc/tutorial/typecasting/
Multiply the float by 255 and then cast it to the char.
Last edited on
Topic archived. No new replies allowed.