Skip to content
Advertisement

How to convert list array to float

I have an little endian list value in python, I get with serial line.

I get this value as a byte like this:

[0, 0, 52, 66]

How can I convert this 4 byte (little endian) value to float ?

Advertisement

Answer

If the float is in IEEE754 format (which it very likely is), you can convert your list of numbers to a byte array and then use struct.unpack to unpack it:

import struct
ba = bytes([0, 0, 52, 66])
print(struct.unpack('<f', ba)[0]) # prints 45.0
Advertisement