Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 851
Francesco Avatar asked Nov 06 '25 02:11

Francesco


1 Answers

Because endianness is a thing.

>>> struct.unpack('>f',hexbytes)
(51.573429107666016,)
like image 160
Ignacio Vazquez-Abrams Avatar answered Nov 07 '25 14:11

Ignacio Vazquez-Abrams