Bug in struct.pack?

A

Alex Stapleton

from struct import pack2

Why does an unsigned char suddenly become 4 bytes long when you
include an unsigned int in the format string? It's consistent
behaviour but it's incorrect.

Also.
'\xff\xff\xff\x00\x00\x00\x00\x00' ####### That's 3 255's and 5(!?!?)
0's'\xff\xff\xff\xff\x00\x00\x00\x00' ######### 4 255's and 4 0's!

Which is all kinds of wrong.

BL should be 9
BBL should be 10
.....

Python 2.4.1 (#2, May 5 2005, 11:32:06)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2

Same behaviour on my PowerBook using

Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin

sizeof(unsigned long) should be 8 on both of these platforms
sizeof(unsigned char) should be 1 on both as well

So am I just being stupid and not specifying something I should be?
Or is struct really that broken?
 
R

Raymond Hettinger

[Alex Stapleton]
from struct import pack
2

Why does an unsigned char suddenly become 4 bytes long when you
include an unsigned int in the format string? It's consistent
behaviour but it's incorrect.

You're seeing native alignment come into play.
Specify a standard alignment to avoid padding.
'\x00\x01\x00\x00\x00'

All is explained in the docs:
http://docs.python.org/lib/module-struct.html


Raymond
 

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

Forum statistics

Threads
474,213
Messages
2,571,109
Members
47,702
Latest member
gancflex

Latest Threads

Top