python ctypes结构错误的字节大小

时间:2014-12-17 08:57:46

标签: python ctypes

所以我想弄清楚为什么ctypes.Structure的大小不应该是它的大小。我的代码如下,还有计算大小应该是什么。

class FILE_HEAD(ctypes.Structure):
    _fields_ = [
        ("name", ctypes.c_char * 4),                    # 4 bytes
        ("size", ctypes.c_int),                         # 4 bytes
        ("Cal_l", ctypes.c_double),                     # 8 bytes
        ("Cal_r", ctypes.c_double),                     # 8 bytes
        ("Speed_ChL", ctypes.c_byte),                   # 1 byte
        ("Speed_Pulses_ChL", ctypes.c_int),             # 4 bytes
        ("Speed_factor_ChL", ctypes.c_double),          # 8 bytes
        ("Quantity_ChL", ctypes.c_char * 3),            # 3 bytes
        ("Description_ChL", ctypes.c_char * 32),        # 32 bytes
        ("Unit_ChL", ctypes.c_char * 8),                # 8 bytes
        ("Speed_ChR", ctypes.c_byte),                   # 1 byte
        ("Speed_Pulses_ChR", ctypes.c_int),             # 4 bytes
        ("Speed_factor_ChR", ctypes.c_double),          # 8 bytes
        ("Quantity_ChR", ctypes.c_char * 3),            # 3 bytes
        ("Description_ChR", ctypes.c_char * 32),        # 32 bytes
        ("Unit_ChR", ctypes.c_char * 8)                 # 8 bytes
    ]                                                   # = 136 bytes

所以我认为Structure的大小应该是136个字节。但是当我让python打印结构print ctypes.sizeof(COMI_HEAD)的一个实例的大小时,我得到144。我不知道那8个字节来自哪里。

我用以下数据填充了它并将其写入文件以查看所有字节并分析字节的位置。

comi = FILE_HEAD()
comi.name = "COMI"
comi.size = ctypes.sizeof(comi) - 8
comi.Cal_l = 342.324
comi.Cal_r = 342.324
comi.Speed_ChL = ctypes.c_byte(1)
comi.Speed_Pulses_ChL = 123
comi.Speed_factor_ChL = 123.456
comi.Quantity_ChL = "Tes"
comi.Description_ChL = "Test Desc"
comi.Unit_ChL = "t/t"
comi.Speed_ChR = ctypes.c_byte(1)
comi.Speed_Pulses_ChR = 123
comi.Speed_factor_ChR = 123.456
comi.Quantity_ChR = "Tes"
comi.Description_ChR = "Test Desc"
comi.Unit_ChR = "t/t"

这是我的HEX-Viewer向我展示的内容。我标记为红色,我认为哪些字节是8个字节太多,但我不知道这8个字节来自哪里。我标记的前3个字节直接来自Speed_ChL,它应该是1个字节但看起来像4个字节。我标记的接下来的5个字节位于文件的末尾。这个字符串Unit_ChR应该是8个字节,但看起来像是13个字节。

enter image description here

有人能告诉我我的错误在哪里吗?我在这做错了什么?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:9)

好的,我已经找到了解决方案。感谢Andreas告诉我,我的问题与offset有关。因此,解决方案是在结构中添加_pack_ = 1

class FILE_HEAD(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("name", ctypes.c_char * 4),                    # 4 bytes
        ("size", ctypes.c_int),                         # 4 bytes
        ("Cal_l", ctypes.c_double),                     # 8 bytes
        ("Cal_r", ctypes.c_double),                     # 8 bytes
        ("Speed_ChL", ctypes.c_byte),                   # 1 byte
        ("Speed_Pulses_ChL", ctypes.c_int),             # 4 bytes
        ("Speed_factor_ChL", ctypes.c_double),          # 8 bytes
        ("Quantity_ChL", ctypes.c_char * 3),            # 3 bytes
        ("Description_ChL", ctypes.c_char * 32),        # 32 bytes
        ("Unit_ChL", ctypes.c_char * 8),                # 8 bytes
        ("Speed_ChR", ctypes.c_byte),                   # 1 byte
        ("Speed_Pulses_ChR", ctypes.c_int),             # 4 bytes
        ("Speed_factor_ChR", ctypes.c_double),          # 8 bytes
        ("Quantity_ChR", ctypes.c_char * 3),            # 3 bytes
        ("Description_ChR", ctypes.c_char * 32),        # 32 bytes
        ("Unit_ChR", ctypes.c_char * 8)                 # 8 bytes
    ]                                                   # = 136 bytes