这行在python 2.7.6中完美运行,但在Python 3.3.5中失败.我如何hex在Python 3中解码为值.
return x.replace(' ', '').replace('\n', '').decode('hex')
Run Code Online (Sandbox Code Playgroud)
追溯
AttributeError: 'str' object has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)
要将十六进制转换为字符串,请使用binascii.unhexlify.
>>> from binascii import unhexlify
>>> unhexlify(x.replace(' ', '').replace('\n', ''))
Run Code Online (Sandbox Code Playgroud)
但是,对于 Python 3,您首先需要使这项工作x正常进行。通过执行以下操作来完成此操作:bytes
>>> x = x.encode('ascii', 'strict')
Run Code Online (Sandbox Code Playgroud)
然后进行十六进制到字符串的转换。