Click to See Complete Forum and Search --> : Conversion of Bytes to IEEE float & double
marcuzlim
May 7th, 2003, 11:47 PM
Hi,
I've a string containing a series of bytes. How do I combine every 4 bytes & return the corresponding IEEE float values? E.g.
char st[] = {0x42,0xC7,0X19,0X9A};
so how do I combine these 4 bytes & get 99.55 as the corresponding decimal values? Hope you can help me. You can also email me at marcuzlim@hotmail.com
pim42
May 15th, 2003, 12:43 PM
I see a bunch of people have looked at this thread, and no responses.
Maybe you could tell us how to get 99.55 from that number conceptually and we can tell you how to do it in code.
What is the format of those numbers? It obviously isn't straight conversion of the numbers and it doesn't seem to be a float equal to the 99.55 when written in binary.
petums
May 22nd, 2003, 08:57 AM
Originally posted by marcuzlim
I've a string containing a series of bytes. How do I combine every 4 bytes & return the corresponding IEEE float values? E.g.
char st[] = {0x42,0xC7,0X19,0X9A};
Here is how to interpret this string of bytes in IEEE standard 32-bit floating point.
The first bit is the sign, the next 8 constitute the exponent, the last 23 are the fraction.
Generally:
Number = -1 ** sign * 2 ** (exp - 127) * (1.fraction)
Here:
...4 ...2 ...c ...7 ...1 ...9 ...9 ...A
0100 0100 1100 0111 0001 1001 1001 1010
seee eeee efff ffff ffff ffff ffff ffff
sign = 0 (it's positive)
exponent = 133
fraction = .10001110001100110011010 (binary) or
1/2 + 1/32 + 1/64 + 1/128 + ... + 1/4194304
Put it all together
-1 ** 0 + 2 ** (133-127) * 1.555468789 = 99.55 (approximately)
There is a complete layout of both the 32 and 64 bit floating point formats here (http://www.psc.edu/general/software/packages/ieee/ieee.html).
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.