I have a binary file that I have to parse and I'm using Python. Is there a way to take 4 bytes and convert it to a single precision floating point number?
75
votes
2 Answers
105
votes
12
votes
Just a little addition, if you want a float number as output from the unpack method instead of a tuple just write
>>> [x] = struct.unpack('f', b'\xdb\x0fI@')
>>> x
3.1415927410125732
If you have more floats then just write
>>> [x,y] = struct.unpack('ff', b'\xdb\x0fI@\x0b\x01I4')
>>> x
3.1415927410125732
>>> y
1.8719963179592014e-07
>>>