python alternatives to C structs??

K

KoolD

Hey all,
I need to convert a C code to python please help me figure out how to
do
it.
Suppose the C program's like:

------------------------------------
typedef struct _str
{
int a;
char *b;
int c;
}str;
int main()
{
str mbr;
fd=open("/dev/sda",O_RDONLY);
read(fd,&mbr,sizeof(str));
}
------------------------------------

Is there a way to code it in python.

Thanks
Sourya
 
A

Aaron Brady

Hey all,
I need to convert a C code to python please help me figure out how to
do
it.
Suppose the C program's like:

------------------------------------
typedef struct _str
{
int a;
char *b;
int c;}str;

int main()
{
str mbr;
fd=open("/dev/sda",O_RDONLY);
read(fd,&mbr,sizeof(str));}

------------------------------------

Is there a way to code it in python.

Thanks
Sourya

There is the 'ctypes' module, but you might also need 'mmap', for the
'read( &mbr )' operation.
 
D

Dave Angel

Aaron said:
There is the 'ctypes' module, but you might also need 'mmap', for the
'read( &mbr )' operation.
A bigger problem is reading that pointer. Reading a pointer from a
device is only meaningful if the device has mapped memory, and then only
if you map it to the same address it was mapped originally.

But getting the ints is pretty easy. Use the struct module. It allows
you to interpret byte strings according to C types.

So do the read with the usual open().read logic. Size could be
anywhere from 6 to 48 bytes, depending on the version and platform of C
you're working from. It might even depend on the alignment settings of
the particular C compile.

Then take the result string, and do a struct.unpack() operation on it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,183
Latest member
GarfieldBa

Latest Threads

Top