Bug in struct.pack?
Alex Stapleton
alexs at advfn.com
Wed Jan 11 05:46:48 EST 2006
from struct import pack
>>> pack("B", 1)
'\x01'
>>> pack("BB", 0, 1)
'\x00\x01'
>>> pack("BI", 0, 1)
'\x00\x00\x00\x00\x01\x00\x00\x00'
>>> calcsize("BI")
8
>>> calcsize("BB")
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.
Also.
>>> calcsize('BL')
8
>>> calcsize('BBL')
8
>>> calcsize('BBBL')
8
>>> calcsize('BBBBL')
8
>>> calcsize('BBBBBL')
12
>>> pack("BBBL", 255,255,255,0)
'\xff\xff\xff\x00\x00\x00\x00\x00' ####### That's 3 255's and 5(!?!?)
0's
>>> pack("BBBBL", 255,255,255,255,0)
'\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?
More information about the Python-list
mailing list