Python2 Python3十六进制索引

2024-09-29 01:32:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个像这样的python十六进制:

variable = b'\x80pu\xa6\x7f\xfe\xb9\x1d\xaf'

在Python2中,如果我使用变量[0],我会得到“\x80”,但在Python3中,它会为我转换为int,并返回128

我想我可以尝试将答案转换回十六进制,如下所示:

hex(variable[0])

但这是一个完整的数组,我可能不是整数。 我一直在寻找更好的方法,因为:

hex() takes only integer and also it returns a string not the hexadecmial version

我想要的是实际的x80而不是128

我该怎么做

我正在使用的代码:

p = b'\x80pu\xa6\x7f\xfe\xb9\x1d\xaf'
f = struct.unpack('>B', p[0])[0]

TypeError: a bytes-like object is required, not 'int'

它在python2中工作,但在Python3中不工作


Tags: notvariablepython3inthex我会x7fpython2