作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下 8 个字节:
b'\x05\x00\x00\x00\x00\x00\x05\x00'
我正在尝试使用 struct.unpack 获取两个整数:一个用于前 2 个字节,一个用于最后 6 个字节。获取前两个很容易使用:
struct.unpack("<H6B")
但是,返回
(5, 0, 0, 0, 0, 5, 0)
我希望它返回以下内容:
(5, 5)
如何获取最后 6 个字节的整数值?我不希望每个字节单独。
最佳答案
struct
不支持非二次方大小的整数。这很常见。 C 在您的平台上也不支持此类整数(好吧,位域,但您不能将它们组成一个数组)。
def unpack48(x):
x1, x2, x3 = struct.unpack('<HHI', x)
return x1, x2 | (x3 << 16)
关于python - 如何在 Python 中使用 struct 将 6 个字节解压缩为单个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7949912/
我是一名优秀的程序员,十分优秀!