Convert hex to float

Viewed 84806

How to convert the following hex string to float (single precision 32-bit) in Python?

"41973333" -> 1.88999996185302734375E1

"41995C29" -> 1.91700000762939453125E1

"470FC614" -> 3.6806078125E4
6 Answers

When working from string and without having to slice or index values.

import struct
...
q = int('0x425c0000',16)
b8 = struct.pack('i', q)
dec, = struct.unpack('f', b8)
Related