interpret 4 byte as 32-bit float (IEEE-754)

Terry Reedy tjreedy at udel.edu
Sat Jan 15 14:36:13 EST 2005


"Scott David Daniels" <Scott.Daniels at Acm.Org> wrote in message 
news:41e952b9$1 at nntp0.pdx.net...
> franzkowiak wrote:
>> I've read some bytes from a file and just now I can't interpret 4 bytes 
>> in this dates like a real value.  An extract from my program:
>> def l32(c):
>>     return ord(c[0]) + (ord(c[1])<<8) + (ord(c[2])<<16) + 
>> (ord(c[3])<<24)
>> ...
>> value = l32(f.read(4))      <---  3F 8C CC CD  should be 1.11
>>
> OK, here's the skinny (I used blocks & views to get the answer):
>
> import struct
> bytes = ''.join(chr(int(txt, 16)) for txt in '3F 8C CC CD'.split())
> struct.unpack('>f', bytes)
>
> I was suspicious of that first byte, thought it might be an exponent,
> since it seemed to have too many on bits in a row to be part of 1.11.

I believe exponents are typically stored as a positive offset from the 
largest negative exponent.  3F8 is about half of 7FF, so that seems about 
right for an actual exponent of 0.

Terry J. Reedy






More information about the Python-list mailing list