struct unpack issue

Discussion in 'Python' started by Peter Otten, Jun 13, 2008.

  1. Peter Otten

    Peter Otten Guest

    Ping Zhao wrote:

    > I am writing a small program to decode MS bitmap image. When I use
    > statements as follow, it works fine:
    >
    > header['sig'] = str(struct.unpack('2s', self.__read(src, 2))[0])
    > header['len'] = int(struct.unpack('1i', self.__read(src, 4))[0])
    >
    > However, when I tried to rewrite them in short:
    >
    > header = struct.unpack('2s1i', self.__read(src, 6))
    >
    > The Python interpreter in my Linux box came up with an error:
    >
    > ...
    > header = struct.unpack('2s1i', self.__read(src, 6))
    > File "/usr/lib/python2.5/struct.py", line 87, in unpack
    > return o.unpack(s)
    > struct.error: unpack requires a string argument of length 8
    >
    > It was weired that the required argument length increased to 8. Any idea
    > on this? I am using a 32bit pentium-m and the picture file was stored in
    > little-edian format.


    Try specifying byte order and alignment:

    >>> struct.unpack("2s1i", "123456")

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.5/struct.py", line 87, in unpack
    return o.unpack(s)
    struct.error: unpack requires a string argument of length 8

    >>> struct.unpack("=2s1i", "123456")

    ('12', 909456435)

    See the second table on http://docs.python.org/lib/module-struct.html for
    the options you have.

    Peter
     
    Peter Otten, Jun 13, 2008
    #1
    1. Advertising

  2. Peter Otten

    Ping Zhao Guest

    -----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['sig'] = str(struct.unpack('2s', self.__read(src, 2))[0])
    header['len'] = int(struct.unpack('1i', self.__read(src, 4))[0])

    However, when I tried to rewrite them in short:

    header = struct.unpack('2s1i', self.__read(src, 6))

    The Python interpreter in my Linux box came up with an error:

    ....
    header = struct.unpack('2s1i', self.__read(src, 6))
    File "/usr/lib/python2.5/struct.py", line 87, in unpack
    return o.unpack(s)
    struct.error: unpack requires a string argument of length 8

    It was weired that the required argument length increased to 8. Any idea on this? I am using a 32bit pentium-m and the picture file was stored in little-edian format.

    Sincerely,

    - --
    Ping Zhao, PhD Student Dormitory: +82-(0)31-338-9503-0415
    Information Engineering
    Myongji University http://oceanio.wordpress.com
    GMT +09:00 (Seoul)

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEARECAAYFAkhTHocACgkQyOZNd2jjOpvTxgCfYnJKZqf4d3Em3xKDcAcpvK8i
    tx4AoI0WvEBv5tvoTQrQlMsj/3UO16t1
    =SGnL
    -----END PGP SIGNATURE-----
     
    Ping Zhao, Jun 14, 2008
    #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,431
    Chris Fogelklou
    Apr 20, 2004
  2. Matthew Barnes
    Replies:
    0
    Views:
    380
    Matthew Barnes
    Nov 19, 2003
  3. mikeSpindler

    struct.unpack() and bit operations

    mikeSpindler, Sep 23, 2004, in forum: Python
    Replies:
    2
    Views:
    584
    Peter Otten
    Sep 23, 2004
  4. Geoffrey
    Replies:
    5
    Views:
    638
    Geoffrey
    Dec 1, 2004
  5. 9bizy

    issue with struct.unpack

    9bizy, Aug 25, 2012, in forum: Python
    Replies:
    16
    Views:
    402
    Dennis Lee Bieber
    Aug 29, 2012
Loading...

Share This Page