low level data types

D

dementrio

How can I handle low-level data types in Python?
What I want to do is writing an interface to a C daemon which waits for
stuff like unsigned ints on a socket. For example, I need to craft and
decode data structures that look like this:

32-bit unsigned int MSG_LENGTH
32-bit unsigned int MSG_CODE
64-bit signed int DATA
32-bit length + utf-8 characters STRING_DATA
etc.

What's the right way to do this in Python?
 
F

Fredrik Lundh

dementrio said:
What I want to do is writing an interface to a C daemon which waits for
stuff like unsigned ints on a socket. For example, I need to craft and
decode data structures that look like this:

32-bit unsigned int MSG_LENGTH
32-bit unsigned int MSG_CODE
64-bit signed int DATA
32-bit length + utf-8 characters STRING_DATA
etc.

What's the right way to do this in Python?

http://docs.python.org/lib/module-struct.html

</F>
 
S

Simon Brunning

How can I handle low-level data types in Python?
What I want to do is writing an interface to a C daemon which waits for
stuff like unsigned ints on a socket. For example, I need to craft and
decode data structures that look like this:

32-bit unsigned int MSG_LENGTH
32-bit unsigned int MSG_CODE
64-bit signed int DATA
32-bit length + utf-8 characters STRING_DATA
etc.

What's the right way to do this in Python?

Look at the struct module.
 
D

dementrio

Thanks for the hint!

However now I have another problem - endianness (the client runs on
powerpc, the server on x86). I found that simply reversing the stuff I
recv() works, but is there any cleaner way for taking care of this?
 
D

Diez B. Roggisch

dementrio said:
Thanks for the hint!

However now I have another problem - endianness (the client runs on
powerpc, the server on x86). I found that simply reversing the stuff I
recv() works, but is there any cleaner way for taking care of this?

Have you actually _read_ the struct documentation?

"""
Alternatively, the first character of the format string can be used to
indicate the byte order, size and alignment of the packed data, according
to the following table:

....

"""

Diez
 
D

Daniel Dittmar

dementrio said:
Thanks for the hint!

However now I have another problem - endianness (the client runs on
powerpc, the server on x86). I found that simply reversing the stuff I
recv() works, but is there any cleaner way for taking care of this?

http://docs.python.org/lib/module-struct.html
and search for "Alternatively, the first character of the format string
can be used to indicate the byte order"

Daniel
 
D

dementrio

Have you actually _read_ the struct documentation?

Ok, sorry about that. Somehow I managed to notice only that "By
default, C numbers are represented in the machine's native format and
byte order".

Now I'm just going to RTFM and stay silent.
 

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

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top