Package: pycstruct
pycstructpycstruct is a python library for converting binary data to and from ordinary
python dictionaries or specific instance objects.Data is defined similar to what is done in C language structs, unions,
bitfields and enums.Typical usage of this library is read/write binary files or binary data
transmitted over a network.Following complex C types are supported:StructsUnionsBitfieldsEnumsThese types may consist of any traditional data types (integer, unsigned integer,
boolean and float) between 1 to 8 bytes large, arrays (lists), and strings (ASCII/UTF-8).Structs, unions, bitfields and enums can be embedded inside other structs/unions
in any level.Individual elements can be stored / read in any byte order and alignment.pycstruct also supports parsing of existing C language source code to
automatically generate the pycstruct definitions / instances.Checkout the full documentationhere.InstallationSimply install the package using pip:python3 -m pip install pycstructExampleFollowing C has a structure (person) with a set of elements
that are written to a binary file.#include<stdio.h>#include<stdbool.h>#include<string.h>#pragma pack(1)// To secure no padding is added in structstructperson{charname[50];unsignedintage;floatheight;boolis_male;unsignedintnbr_of_children;unsignedintchild_ages[10];};voidmain(void){structpersonp;memset(&p,0,sizeof(structperson));strcpy(p.name,"Foo Bar");p.age=42;p.height=1.75;// mp.is_male=true;p.nbr_of_children=2;p.child_ages[0]=7;p.child_ages[1]=9;FILE*f=fopen("simple_example.dat","w");fwrite(&p,sizeof(structperson),1,f);fclose(f);}To read the binary file using pycstruct following code
required.importpycstructperson=pycstruct.StructDef()person.add('utf-8','name',length=50)person.add('uint32','age')person.add('float32','height')person.add('bool8','is_male')person.add('uint32','nbr_of_children')person.add('uint32','child_ages',length=10)withopen('simple_example.dat','rb')asf:inbytes=f.read()# Dictionary representationresult=person.deserialize(inbytes)print('Dictionary object:')print(str(result))# Alternative, Instance representationinstance=person.instance(inbytes)print('\nInstance object:')print(f'name:{instance.name}')print(f'nbr_of_children:{instance.nbr_of_children}')print(f'child_ages[1]:{instance.child_ages[1]}')The produced output will be::{'name': 'Foo Bar', 'is_male': True, 'nbr_of_children': 2, 
 'age': 42, 'child_ages': [7, 9, 0, 0, 0, 0, 0, 0, 0, 0], 
 'height': 1.75}

Instance object:
name: Foo Bar
nbr_of_children: 2
child_ages[1]: 9To write a binary file from python using the same structure
using pycstruct following code is required.importpycstructperson=pycstruct.StructDef()person.add('utf-8','name',length=50)person.add('uint32','age')person.add('float32','height')person.add('bool8','is_male')person.add('uint32','nbr_of_children')person.add('uint32','child_ages',length=10)# Dictionary representationmrGreen={}mrGreen['name']="MR Green"mrGreen['age']=50mrGreen['height']=1.93mrGreen['is_male']=TruemrGreen['nbr_of_children']=3mrGreen['child_ages']=[13,24,12]buffer=person.serialize(mrGreen)# Alternative, Instance representationmrGreen=person.instance()mrGreen.name="MR Green"mrGreen.age=50mrGreen.height=1.93mrGreen.is_male=TruemrGreen.nbr_of_children=3mrGreen.child_ages[0]=13mrGreen.child_ages[1]=24mrGreen.child_ages[2]=12buffer=bytes(mrGreen)# Write to filef=open('simple_example_mr_green.dat','wb')f.write(buffer)f.close()Parsing source filespycstruct also supports parsing C source code defined in external
files or defined in strings.Assume the C code listed in the first example is named
simple_example.c. Then you could parse the source
code instead of manually creating the definitions:importpycstructdefinitions=pycstruct.parse_file('simple_example.c')withopen('simple_example.dat','rb')asf:inbytes=f.read()# Dictionary representationresult=definitions['person'].deserialize(inbytes)print(str(result))# Alternative, Instance representationinstance=definitions['person'].instance(inbytes)The produced output will be the same as in the first example (the dictionary).Full documentationCheckout the full documentationhere.Author and licenseThis application is written by Joel MidstjÃ¤rna and is licensed under the MIT License.