Python 3中类“字节”的不同表示形式

时间:2018-09-26 08:20:02

标签: python python-3.x class bytestring

在python3中,我有两个bytes类的变量。

print(string1) --> b'2900BCE03604093C000080'
print(bytes.fromhex(string1.decode('utf8'))) --> b')\x00\xbc\xe06\x04\t<\x00\x00\x80'

print(type(string1)) --> <class 'bytes'>
print(type(bytes.fromhex(string1.decode('utf8')))) --> <class 'bytes'>

由于一些十六进制值的ascii解释,第二个输出中的奇怪值在那里。

我的问题是如何更轻松地将string1转换为第二行的输出。有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

您可以使用binascii.a2b_hex()函数来获取二进制数据的十六进制表示形式:

In [5]: binascii.a2b_hex(s)
Out[5]: b')\x00\xbc\xe06\x04\t<\x00\x00\x80'
相关问题