简体   繁体   English

如何将列表数组转换为浮点数

[英]How to convert list array to float

I have an little endian list value in python, I get with serial line.我在 python 中有一个小端序列表值,我用串行线得到。

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?如何将此 4 字节(小端)值转换为浮点数?

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:如果浮点数是 IEEE754 格式(很可能是),您可以将数字列表转换为字节数组,然后使用struct.unpack进行解包:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM