473,498 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unpacking problem

What's wrong here?
>>from struct import unpack
I can unpack an unsigned char
>>unpack('B','\x90')
(144,)

I can unpack a short
>>unpack('h','\x06\x00')
(6,)

But an unsigned char & a short give me this
>>unpack('Bh','\x90\x06\x00')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format
>>>
Mar 1 '07 #1
3 1915
In <11*********************@h3g2000cwc.googlegroups.c om>, Chris Garland
wrote:
But an unsigned char & a short give me this
>>>unpack('Bh','\x90\x06\x00')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format
Let's pack this:

In [90]: pack('Bh', 0x90, 0x6)
Out[90]: '\x90\x00\x06\x00'

Per default the values are (un)packed with the preferred alignment of the
C compiler, so here is an extra byte to place the short at an even
address. If you give the endianess in the format string there's no
padding:

In [91]: pack('<Bh', 0x90, 0x6)
Out[91]: '\x90\x06\x00'

Ciao,
Marc 'BlackJack' Rintsch
Mar 1 '07 #2
The problem is, that len('\x90\x06\x00') is not equivalent to
calcsize('Bh'):
>>calcsize('Bh')
4
>>len('\x90\x06\x00')
3

Actually calculating the size for 'hB' results in:
>>calcsize('hB')
3

So far I have not figured out, why there is an additional byte, but it
does not effect the result in any way. (Or I simply did not insert any
byte that would cause an effect.)

So
>>unpack('Bh', '\x90\x??\x06\x00')
(144,6)

with ?? as any valid hex, gives the "correct" result, but I dont know
why either.

On Mar 1, 10:17 pm, "Chris Garland" <chrisgarlan...@gmail.comwrote:
What's wrong here?
>from struct import unpack

I can unpack an unsigned char>>unpack('B','\x90')

(144,)

I can unpack a short>>unpack('h','\x06\x00')

(6,)

But an unsigned char & a short give me this>>unpack('Bh','\x90\x06\x00')

Traceback (most recent call last):
File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format


Mar 1 '07 #3
Ok, that solves my confusion.
Thanks, Marc.

Mar 1 '07 #4

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

Similar topics

32
2792
by: Dave Benjamin | last post by:
Hey all, I just realized you can very easily implement a sequence grouping function using Python 2.3's fancy slicing support: def group(values, size): return map(None, * for i in...
0
1348
by: Victor S. Miller | last post by:
I'm sure that this has been asked and answered before, but about 1/2 hour of searching hasn't turned up anything. I have external data that requires the packing and unpacking of long vectors of...
2
1749
by: george young | last post by:
I came across an cool python feature that I've not seen discussed. This may be *implied* by the language reference manual http://docs.python.org/ref/assignment.html], but it was never obvious to...
8
3609
by: Paul McGuire | last post by:
I'm trying to manage some configuration data in a list of tuples, and I unpack the values with something like this: configList = for data in configList: name,a,b,c = data ... do something...
5
1971
by: Chris | last post by:
Hi I'm attempting to write a client for an existing p2p network. The protocol defines that ints are packed into 4 bytes for transfer. // Creating the byte vector using the following is fine:...
4
3351
by: Claudio Grondi | last post by:
I need to unpack on a Windows 2000 machine some Wikipedia media .tar archives which are compressed with TAR 1.14 (support for long file names and maybe some other features) . It seems, that...
9
1404
by: tkpmep | last post by:
I have a list y >>>y from which I want to extract only the 2nd and 4th item by partially unpacking the list. So I tried >>>a,b = y Traceback (most recent call last): File "<interactive...
16
1318
by: John Salerno | last post by:
I'm a little confused, but I'm sure this is something trivial. I'm confused about why this works: ('more', 'less'), ('something', 'nothing'), ('good', 'bad')) (('hello', 'goodbye'), ('more',...
5
1504
by: ram | last post by:
Stupid question #983098403: I can't seem to pass an unpacked sequence and keyword arguments to a function at the same time. What am I doing wrong? def f(*args, **kw): for a in args: print...
21
7223
by: Martin Geisler | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4 0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY =hTwE -----END PGP...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.