1

使用 python 结构pack('<B',1)值正确打包<01>到一个字节,但使用 ctypes 我无法获得类似的结果。

是否可以使用 ctypes 获得相同的结果?

c_byte似乎是 4 个字节<01000000>

添加了示例代码。

class TEST(Structure):
    _fields_ = [("int", c_int),("byte", c_byte)]

test = TEST(2,1)
print test.int
print test.byte

#bytes
print hexlify(buffer(test)[:])

现在打印是

2
1
0200000001000000

字节应该是 0200000001。是因为缓冲区调用还是我应该以某种方式声明字节对齐?

4

1 回答 1

2

这可能是因为对齐/填充,使用_pack_设置:

class TEST(Structure):
    _pack_ = 1
    _fields_ = [("int", c_int),("byte", c_byte)]

test = TEST(2,1)    
print hexlify(test)

将打印0200000001

于 2012-11-14T11:56:31.470 回答