Search code examples
pythonieee-754

Converting hexadecimal to IEEE 754


If I use a website like http://www.h-schmidt.net/FloatConverter/IEEE754.html to convert the hex string '424E4B31' into float32, I get 51.57343.

I need to use Python to convert the string, however, using solutions on StackExchange like:

import struct, binascii
hexbytes = b"\x42\x4E\x4B\x31"
struct.unpack('<f',hexbytes)

or

struct.unpack('f', binascii.unhexlify('424E4B31'))

I get 2.9584e-09... why is it different?


Solution

  • Because endianness is a thing.

    >>> struct.unpack('>f',hexbytes)
    (51.573429107666016,)