Bug in struct.pack?

Discussion in 'Python' started by Alex Stapleton, Jan 11, 2006.

  1. 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?
     
    Alex Stapleton, Jan 11, 2006
    #1
    1. Advertising

  2. [Alex Stapleton]
    > 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.


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

    >>> pack("BI", 0, 1)

    '\x00\x00\x00\x00\x01\x00\x00\x00'
    >>> pack("=BI", 0, 1)

    '\x00\x01\x00\x00\x00'

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


    Raymond
     
    Raymond Hettinger, Jan 11, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,405
    Chris Fogelklou
    Apr 20, 2004
  2. Tim Jones
    Replies:
    0
    Views:
    396
    Tim Jones
    Jan 31, 2004
  3. Jansson Christer

    struct.pack bug?

    Jansson Christer, Oct 27, 2006, in forum: Python
    Replies:
    2
    Views:
    994
    Jansson Christer
    Oct 27, 2006
  4. Jonathan Fine

    struct.pack bug?

    Jonathan Fine, Feb 8, 2007, in forum: Python
    Replies:
    1
    Views:
    369
    John Machin
    Feb 9, 2007
  5. Alexander Farber

    pack 'C3U*' not same as pack 'C3(xC)*'

    Alexander Farber, Jun 23, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    153
    Ilmari Karonen
    Jun 23, 2005
Loading...

Share This Page