我正在尝试将 IEEE 754 十六进制 float 转换为标准 python float 。
以下在 Python 2.x 中有效:
foo ='4074145c00000005'
conv_pound = struct.unpack('!d', foo.decode('hex'))[0]
print(conv_pound)
并产生以下输出(这确实是我想要的数字):
321.272460938
但是,python 3 没有 str.decode 方法,我正在努力寻找如何做到这一点。有什么建议吗?
bytes.fromhex()
在 python3 中为我工作:
Python 3.6.6 (default, Sep 12 2018, 18:26:19)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> foo ='4074145c00000005'
>>> import struct
>>> struct.unpack('!d', bytes.fromhex(foo))
(321.2724609375003,)
>>> struct.unpack('!d', bytes.fromhex(foo))[0]
321.2724609375003