为什么整数在 python 3 中有不同的十六进制值?
Why do ints have different hex values in python 3?
我正在将一些代码从 python 2.7 转换为 python 3,我 运行 遇到十六进制整数问题。
value = 0x11
hexdump(value)
python2 output: 11
python3 output: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
这个 hexdump 函数来自 scapy,对我所有其他的 hex 故障排除都有效。执行 chr(0x11) 会产生预期值,但如果它不是 int 类型,许多 scapy 函数会被调用而崩溃。
我需要一种方法来获取 11 作为我的 int 类型的十六进制数据。我尝试使用
更改类型
int(value,16) #also tried 0 and 10
问题是我得到 ValueError: invalid literal for int() with base 10:
'\x11'
0x11
在 python2 和 python3 中都是一个整数。也许您的 hexdump
函数中的某些字符串处理出了问题? python3 中的字符串非常 不同。
你能 post hexdump()
的代码吗?
根据@Cukic0d 下面的评论,如果您的 hexbytes()
实际上想要一个字节数组,这应该可以工作: hexdump(bytes([value]))
-- 转换包含值的长度为 1 的整数数组进入 bytes
对象并将其传递给 hexdump
.
我正在将一些代码从 python 2.7 转换为 python 3,我 运行 遇到十六进制整数问题。
value = 0x11
hexdump(value)
python2 output: 11
python3 output: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
这个 hexdump 函数来自 scapy,对我所有其他的 hex 故障排除都有效。执行 chr(0x11) 会产生预期值,但如果它不是 int 类型,许多 scapy 函数会被调用而崩溃。
我需要一种方法来获取 11 作为我的 int 类型的十六进制数据。我尝试使用
更改类型 int(value,16) #also tried 0 and 10
问题是我得到 ValueError: invalid literal for int() with base 10: '\x11'
0x11
在 python2 和 python3 中都是一个整数。也许您的 hexdump
函数中的某些字符串处理出了问题? python3 中的字符串非常 不同。
你能 post hexdump()
的代码吗?
根据@Cukic0d 下面的评论,如果您的 hexbytes()
实际上想要一个字节数组,这应该可以工作: hexdump(bytes([value]))
-- 转换包含值的长度为 1 的整数数组进入 bytes
对象并将其传递给 hexdump
.