【发布时间】:2012-11-14 11:18:59
【问题描述】:
使用 python struct 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。是因为缓冲区调用还是我应该以某种方式声明字节对齐?
【问题讨论】:
-
你从哪里得到输出?
ctypes.sizeof(ctypes.c_byte(1))为我返回 1。