I'm trying using the following code:
float fValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:@"0x41CC8937"];
[scanner scanHexFloat:&fValue];
Hex 0x41CC8937 = float 25.567.
But I'm getting fValue = 0x4E839912 (float 1103923456.000000), Why?
scanHexFloat
expects a "Hexadecimal Floating-Point Constant", you can read more about that here: http://www.exploringbinary.com/hexadecimal-floating-point-constants/.
What you have it the binary representation of the 32-bit floating point value. You can read
that with scanHexInt
and use a union to interpret the number as float
:
union {
float fValue;
unsigned int iValue;
} u;
NSScanner *scanner = [NSScanner scannerWithString:@"0x41CC8937"];
[scanner scanHexInt:&u.iValue];
NSLog(@"%f", u.fValue);
Output: 25.566999