I would like to know how to convert a float number (value is always between 0 and 255) to a byte like this :
Float value 5,023 should be converted to 5 in my byte, 127.845 => 127 etc.
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
Posted: Mon Aug 30, 2010 3:54 pm
In the general C language lingo you want to "cast" the float to an 8 bit int. The compiler should do this automatically, but to be really sure you can do what mkuang shows.
This
my_integer = (unsigned char) my_float;
is an explicit cast, whereas simply
my_integer = my_float;
should do an automatic cast. _________________ The search for better is endless. Instead simply find very good and get the job done.
tahiticecil
Joined: 30 Aug 2010 Posts: 2
Posted: Tue Aug 31, 2010 5:23 am
Hi !
I hadn't thought that a simple cast could work. I tried to work directly on each bytes of a float var to get the integer part.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum