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
182
Posting Pro in Training
Featured Poster
Recommended Answers
Jump to PostAn 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;
All 2 Replies
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.