我有十六进制值4396 eccd
。如果我使用一些在线计算器将其转换为浮点数301.85
,则会得到正确的值。
但是当我使用python转换它时,我得到了一些不同的值:
>>> float.fromhex('0x4396eccd')
1133964493.0
谁能帮助我,为什么它在python中显示错误的值。谢谢
要了解做fromhex()
什么,可以参考以下内容:https : //python-reference.readthedocs.io/en/latest/docs/float/fromhex.html
与其使用fromhex()
十六进制字符串来浮动数字,不如使用struct
模块。
在python 2.x中
>>> import struct
>>> struct.unpack('!f', '41973333'.decode('hex'))[0]
18.899999618530273
在python 3.x中使用:
bytes.fromhex('41973333')
代替 '41973333'.decode('hex')
因此将如下所示:
>>> import struct
>>> struct.unpack('!f', bytes.fromhex('41973333'))[0]
18.899999618530273
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句