我试图在Python3.8中解压python结构,并得到错误信息
TypeError: a bytes-like object is required, not 'int'
。同样的代码在Python2.7中运行良好
import struct
hexval= b'J\xe6\xe7\xa8\x002\x10k\x05\xd4\x7fA\x00\x04\n\x90\x1a\n'
aaT = struct.unpack('>H',hexval[4:6])
aa = aaT[0]
print("aa",aa)
bbT = struct.unpack(">B",hexval[12])
bb = bbT[0]&0x3 # just lower 2 bits
print("bb",bb)
输出:
aa 50
回溯(最近一次呼叫最后一次): 文件“/sample.py”,第9行,在 bbT=struct.unpack(“>;B”,hexval[12]) TypeError:需要类似字节的对象,而不是“int”
当我转换成字节时
我得到这样的错误
Traceback (most recent call last): File "sample.py", line 9, in bbT = struct.unpack(">B",bytes(hexval[12])) struct.error: unpack requires a buffer of 1 bytes
我如何解包这个二进制数据
这是与从Python2到Python3的数据类型相关的另一个变化。对Why do I get an int when I index bytes?的答复解释了这一推理
万一答案不明显,要获得与Python 2中相同的结果,请执行以下操作:
相关问题 更多 >
编程相关推荐