954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Char to Float

This is a conversion question that has me stumped. I need to convert an unsigned char to a signed float. Some examples:
0xFF becomes 1.0f
0x00 becomes -1.0f
0x80 becomes ~0.0f
The only idea that I could come up with was a massive switch statement. But a switch statement will be slow and I need this to run fast. Any ideas?

Labdabeta
Posting Pro in Training
489 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

An unsigned char is just a regular unsigned integer going from 0 to 255. Just interpret it as a regular "number" and map 0 to -1.0 and 255 to 1.0 with a simple linear mapping:

unsigned char u;
  float f;
  f = float(u) / 127.5 - 1.0;
mike_2000_17
Posting Virtuoso
Moderator
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

Perhaps have a look at this ?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: