473,382 Members | 1,431 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,382 software developers and data experts.

Question about struct.unpack

Hi,

To experiment with unpacking, i've written a little C code which
stores one record in a file. Then, i try to reread this file to unpack
the record.

Here's the struct of a record:

typedef struct {
char nom[30];
double taille;
int age;
char plop;
} enreg_t;

The whole size, as given by sizeof() is 48, due to byte alignment.

I was first thinking that "32sdic" would make the job, but calcsize()
reports only 45 for this format. So, without knowing what, i've tried
"32sdicxxx" to reach the 48 expected... Now it works...

The same file, re-read with a Ruby script needs a
str.unpack("Z32dIc").

So, i don't know why i need to pad the format string in Python. Any
clue?

BTW: how to get rid of all this stuff after the \0 in the first field
in Python? (Ruby has Z and A, but it seems that the Python 's'
specifier is like 'A' and there is no 'Z' equivalent)
--
Eric Jacoboni, ne il y a 1444057108 secondes
Feb 22 '06 #1
3 5487
Eric Jacoboni wrote:
Hi,

To experiment with unpacking, i've written a little C code which
stores one record in a file. Then, i try to reread this file to unpack
the record.

Here's the struct of a record:

typedef struct {
char nom[30];
double taille;
int age;
char plop;
} enreg_t;

The whole size, as given by sizeof() is 48, due to byte alignment.
...i've tried "32sdicxxx" to reach the 48 expected... Now it works...

The same file, re-read with a Ruby script needs a
str.unpack("Z32dIc").

So, i don't know why i need to pad the format string in Python. Any
clue?

BTW: how to get rid of all this stuff after the \0 in the first field
in Python? (Ruby has Z and A, but it seems that the Python 's'
specifier is like 'A' and there is no 'Z' equivalent)


OK, the correct translation of your format is: '30sdic' (size 45) or
'30sdic3x' if you are passing the entire 48-char block. The reason
it is not size 48 is that '30sdicc' (size 46) in C takes no more room.
the alignment to the end shows up in a C sizeof.

data = struct.pack('30sdic', 'John Q. Public', 57123.25, 43, 'M')
nomz, taille, age, plop = struct.unpack('30sdic', data)
nom = nomz.rstrip('\0')

--Scott David Daniels
sc***********@acm.org
Feb 22 '06 #2
Scott David Daniels <sc***********@acm.org> writes:

Thanks for your explanations.

But :
nom = nomz.rstrip('\0')


doesn't work for me:
nomz 'Dupont\x00\x80\xbf\xff\xf70\x8f\xe0u\xa4\x00\x00. 8\xfe\xfe\xfe\xff\x80\x80\x80\x80'
nom = nomz.rstrip('\0')
nom

'Dupont\x00\x80\xbf\xff\xf70\x8f\xe0u\xa4\x00\x00. 8\xfe\xfe\xfe\xff\x80\x80\x80\x80'
--
Eric Jacoboni, ne il y a 1444080064 secondes
Feb 22 '06 #3
Eric Jacoboni wrote:
But :
nom = nomz.rstrip('\0')


doesn't work for me:
nomz 'Dupont\x00\x80\xbf\xff\xf70\x8f\xe0u\xa4\x00\x00. 8\xfe\xfe\xfe\xff\x80\x80\x80\x80'
nom = nomz.rstrip('\0')
nom

'Dupont\x00\x80\xbf\xff\xf70\x8f\xe0u\xa4\x00\x00. 8\xfe\xfe\xfe\xff\x80\x80\x80\x80'


Sorry, I thought you had NUL-filled data.
For NUL terminated data:
nom = nomz[: nomz.index('\0')]

--Scott David Daniels
sc***********@acm.org
Feb 22 '06 #4

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

Similar topics

5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
5
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are...
10
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it...
3
by: David Bear | last post by:
I found this simple recipe for converting a dotted quad ip address to a string of a long int. struct.unpack('L',socket.inet_aton(ip)) trouble is when I use this, I get struct.error: unpack...
3
by: Steven W. Orr | last post by:
I have a tuple that I got from struct.unpack. Now I want to pass the data from the returned tuple to struct.pack 'l 10l 11i h 4h c 47c 0l' Traceback (most recent call last): File "<stdin>",...
4
by: OhKyu Yoon | last post by:
Hi! I have a really long binary file that I want to read. The way I am doing it now is: for i in xrange(N): # N is about 10,000,000 time = struct.unpack('=HHHH', infile.read(8)) # do...
2
by: brnstrmrs | last post by:
If I run: testValue = '\x02\x00' junk = struct.unpack('h', testValue) Everything works but If I run testValue = raw_input("Enter Binary Code..:") inputting at the console '\x02\x00' junk...
0
by: Ping Zhao | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine: header = str(struct.unpack('2s',...
2
by: Heikki Toivonen | last post by:
M2Crypto has some old code that gets and sets socket timeouts in http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py, for example: def get_socket_read_timeout(self): return...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.