Python 3相当于Python 2 str.decode('hex')

use*_*975 6 python decode python-2.7 python-3.x

我正在尝试将十六进制的IEEE 754浮点数转换为标准的python浮点数.

以下适用于Python 2.x:

foo ='4074145c00000005'
conv_pound = struct.unpack('!d', foo.decode('hex'))[0]
print(conv_pound)
Run Code Online (Sandbox Code Playgroud)

并产生以下输出(这确实是我想要的数字):

321.272460938
Run Code Online (Sandbox Code Playgroud)

但是,python 3没有str.decode方法,我很难找到如何做到这一点.有小费吗 ?

Sat*_*evg 8

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
Run Code Online (Sandbox Code Playgroud)