Francesco
Francesco

Reputation: 390

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?

Upvotes: 4

Views: 2709

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798726

Because endianness is a thing.

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

Upvotes: 6

Related Questions