473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

low level data types

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?

Feb 15 '06 #1
7 2014
"dementrio" wrote:
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>

Feb 15 '06 #2
On 15 Feb 2006 04:07:37 -0800, dementrio <de*******@gmail.com> wrote:
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.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Feb 15 '06 #3
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?

Feb 15 '06 #4
dementrio wrote:
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
Feb 15 '06 #5
"dementrio" wrote:
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

(see the second table, the one that has "byte order" and "size and
alignment" in the heading)

</F>

Feb 15 '06 #6
dementrio wrote:
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
Feb 15 '06 #7
> 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.

Feb 15 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Activator8 | last post by:
In our application, we have noticed on two rare occasion where the data stored in application level arrays has disappeared. We have 3 application level arrays, and many other data types stored at...
8
by: York | last post by:
Hi, R language has very high-level IO functions, its read.table can read a total .csv file and recogonize the types of each column. write.table can do the reverse. R's MySQL interface has...
2
by: Nathan | last post by:
I've spent a good part of the afternoon searching Google and the newsgroups for some sort of answer - so I apologize if I'm asking something that has already been asked and answered. I'm using...
27
by: Madhav | last post by:
Hi all, I did not understand why do the global vars are initialized to NULL where as the block level variables have random values? I know that the C standard requires this as was mentioned in a...
8
by: mesut | last post by:
Mybe it's a stupid question but : I'm starting to learn VB.NET. I have some basic skills. I'm from Mainframe world and I have same question to make my DATABASES (tables and relation) I would like...
18
by: Jacek Dziedzic | last post by:
Hi! I'm trying to squeeze a few clock cycles from a tight loop that profiling shows to be a bottleneck in my program. I'm at a point where the only thing that matters is execution speed, not...
65
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective...
5
emaghero
by: emaghero | last post by:
Greetings and Salutations, I am currently doing some file I/0 on data that I have and I do not wish to use fstream. I have data stored in its untranslated binary format and I want to read it into...
6
by: gskbond | last post by:
Following is my logic to traverse level by level an mway search tree.... (Yeah I finally used templates to implement a queue... :) ) void mWayTree:: TraverseLevelOrder(Node *root,int lvl) {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.