在Python 2,得到的字符串中的十六进制数字的字符串表示,你可以做如何在Python 3.2或更高版本中使用'十六进制'编码?
>>> '\x12\x34\x56\x78'.encode('hex')
'12345678'
在Python 3,不工作了(关于Python 3.2和3.3测试):
>>> '\x12\x34\x56\x78'.encode('hex')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
LookupError: unknown encoding: hex
至少有one answer这里上,使得提到,hex
编解码器在Python 3被移除但随后,according to the docs,有人在Python 3.2重新引入,作为“字节至字节映射”。
不过,我不知道如何让这些“字节到字节映射”的工作:
>>> b'\x12'.encode('hex')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'encode'
而且文档不提,要么(至少不是我看了) 。我必须错过简单的东西,但我看不到它是什么。
看到这样的回答:HTTP:// stackoverflow.com/a/2340358/1298523 – scape
我会争辩反对把它当作愚蠢的东西来关闭这个问题特别关于Python 3.2,其中'hex'编解码器正式倒退(但很难找到)。链接的问题是关于Python 3.1的。 –