所以我试图弄清楚为什么 ctypes.Structure 的大小不是应该的大小。我的代码如下,还计算了afaik的大小。
So I'm trying to figure out why the size of my ctypes.Structure is not what it should be. My code is the following, theres also the calculation of what the size should be afaik.
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个字节是从哪里来的。
So I think that Structure should have a size of 136 bytes. But when I let python print the size of an instance of the structure print ctypes.sizeof(COMI_HEAD) I get 144. I do not know where those 8 bytes come from.
我用以下数据填充了该数据,并将其写到文件中以查看所有字节并分析其中的字节个字节。
I've filled that with the following data and wrote that to a file to see all the bytes and analyze where the bytes are.
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个字节。
Here is what my HEX-Viewer shows me. I've marked red, which bytes I think are the 8 bytes that are too much, but I do not know where these 8 bytes come from. The first 3 bytes I marked come directly after the Speed_ChL which should be 1 byte but looks like its 4 bytes. The next 5 bytes I've marked are at the end of the file. Theres this string Unit_ChR which should be 8 bytes but looks like it is 13 bytes.
有人可以告诉我我的错误在哪里吗?我在这里做错了什么?感谢您的帮助!
Can someone tell me where my mistake is? What am I doing wrong here? Any help is appreciated!
推荐答案好的,所以我找到了解决方案。感谢Andreas告诉我,我的问题与偏移量。因此解决方案是在这样的结构中添加 _pack_ = 1 。
Ok so I've found the solution. Thanks to Andreas who told me that my problem has to do with the offset. So the solution is to add a _pack_ = 1 to the structure like that.
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更多推荐
python ctypes结构错误的字节大小
发布评论