gpt4 book ai didi

python - 如何在 Python 中使用 struct 将 6 个字节解压缩为单个整数

转载 作者:太空狗 更新时间:2023-10-29 21:22:55 24 4
gpt4 key购买 nike

我有以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com