Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use c structure in python "?

Tags:

python

c

wrapper

I have a simple C function.

typedef struct {
    long unsigned int First;
    long unsigned int Second;
    int c;
} FRAGMENTS;

struct out {
    long unsigned int Four;
    FRAGMENTS fragments[10000];
};
struct out test() {
    struct out *out = (struct out *)malloc(sizeof(struct out));
    ...
    return *out
}

How to use this function in Python ? Any example for transform this structure to python object ( using python wrapper ) ?

like image 892
Bdfy Avatar asked Sep 28 '26 22:09

Bdfy


2 Answers

Do you have the data already "in" Python (i.e. from the network or a binary file)? Than you an use the struct module.

like image 131
Martin Thurau Avatar answered Sep 30 '26 13:09

Martin Thurau


The easiest way is to use SWIG to generate a Python wrapper around your C code. You can also use it to generate bindings/wrappers for lots of other scripting languages to.

like image 22
James O'Doherty Avatar answered Sep 30 '26 14:09

James O'Doherty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!