Search code examples
python-2.7floating-point32-bit

Reading a 32 bit floating point binary data file (little endian)


I want to read a binary data file that contains 32 bit floating point binary data in python. I tried using hexdump on the file and then reading the hexdump in python. Some of the values when converted back to float returned nan. I checked if I made a mistake in combining the hexdump values but couldn't find any. This is what I did this in shell:

hexdump -vc >> output.txt

The output was of the form c0 05 e5 3f ... and so on

I joined the hex as : '3fe505c0'

Is this the correct way to do this ?


Solution

  • No.

    >>> import struct
    >>> struct.unpack('<f', '\xc0\x05\xe5\x3f')
    (1.7892379760742188,)