The correct way to get the value transferred as accurately as float
will allow is:
float myFloat = myInt;
If you want better accuracy, use double
instead of float
.
What you're doing is trying to reinterpret the bit pattern for the int
as if it was a float
, which is not a good idea. There are hexadecimal floating-point constants and conversions available in C99 and later. (However, if that's what you are trying, your code in the question is correct — your problem appears to be in converting hex to integer.)
If you get -2147483648
from 0x0000FFFF (or from 0x7EEEFFFF), there is a bug in your conversion code. Fix that before doing anything else. How are you doing the hex to integer conversion? Using strtol()
is probably a good way (and sscanf()
and friends is also possible), but be careful about overflows.)