Python ctypes pragma pack for byte aligned

I have a C ++ application with the underlying structure written to a file. Now I need to unmount them using python. The main problem is how to reflect a parameter pragma packin python.

C ++ structure

#pragma pack(1)
struct abc  
{  
unsigned char r1;  
unsigned char r2;  
unsigned char p1;  
unsigned int id;  
};  
#pragma pack()

Now the size of the structure 7 not 8, this data is written to the data file. How to get this data using python.

Note:
1. I am using ctypes, and the above structure is a sampling structure.


ctypes uses its own byte order for structures and unions. To build structures with non-local byte order, you can use one of the base classes BigEndianStructure, LittleEndianStructure, BigEndianUnion and LittleEndianUnion. These classes cannot contain pointer fields.


python .

+5
1

ctypes,

Structure Union , C. , . . , #pragma pack (n) MSVC.

:

from ctypes import *

class abc(Structure):
    _pack_ = 1
    _fields_ = [
        ('r1',c_ubyte),
        ('r2',c_ubyte),
        ('p1',c_ubyte),
        ('id',c_uint)]
+11

All Articles