将十六进制字符串转换为 bytes 函数的正确形式
Converting a hex string into the correct form for the bytes function
我从函数中得到两个十六进制字符串:
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
returns 字符串中使用的hex
函数。 bytes
想要一个整数列表。解决方案是使用 int
.
将字符串转换为整数
我从函数中得到两个十六进制字符串:
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
returns 字符串中使用的hex
函数。 bytes
想要一个整数列表。解决方案是使用 int
.