我从函数中得到两个十六进制字符串:
def getHex(hexIn):
return hex(hexIn >> 8), hex(hexIn & 0xFF)
然后我想这样做:
Hi, Lo = getHex(14290)
Cmd = bytes([0x66, 0x44, 0xA6, Hi, Lo])
但是我得到了错误:
TypeError: 'str' object cannot be interpreted as an integer
如何将其转换为类似0x66 的形式?
请您参考如下方法:
你报错说明你使用的是python3
替换:
Cmd = bytes([0x66, 0x44, 0xA6, Hi, Lo])
与:
Cmd = bytes([0x66, 0x44, 0xA6, int(Hi, 16), int(Lo, 16)])
getHex 中使用的hex 函数返回字符串。 bytes 想要一个整数列表。解决方案是使用 int 将字符串转换为整数。
