How to read an integer value from a binary file?

Discussion in 'Python' started by Olivier Elbaz, May 3, 2005.

  1. Hi,

    I read a bmp file (binary file) and I want to extract an integer value
    from it (size value).
    The whole binary file is read inside a list "all_bmp_file".
    I can read the "size" value with the following code:
    for r in all_bmp_file[2:5]:
    n = struct.unpack('B',r)#B for unsigned char
    print n
    Result:
    (182,)
    (47,)
    (0,)
    The problem is that I would like to read the 4 bytes (integer) as one
    number like:
    struct.unpack('i','\xb6/\x00\x00')
    Result
    (12214,)
    Note that I can't use struct.unpack('i',all_bmp_file[2:5]) inside my
    program since I got a syntax error.

    Could you help me to find a solution?
    Thanks,
    Olivier
    Olivier Elbaz, May 3, 2005
    #1
    1. Advertising

  2. Olivier Elbaz

    John Machin Guest

    On Tue, 3 May 2005 19:46:17 +0200, Olivier Elbaz
    <> wrote:

    >Hi,
    >
    >I read a bmp file (binary file) and I want to extract an integer value
    >from it (size value).
    >The whole binary file is read inside a list "all_bmp_file".
    >I can read the "size" value with the following code:
    >for r in all_bmp_file[2:5]:
    > n = struct.unpack('B',r)#B for unsigned char
    > print n
    >Result:
    >(182,)
    >(47,)
    >(0,)
    >The problem is that I would like to read the 4 bytes (integer) as one
    >number like:
    >struct.unpack('i','\xb6/\x00\x00')
    >Result
    >(12214,)
    >Note that I can't use struct.unpack('i',all_bmp_file[2:5]) inside my
    >program since I got a syntax error.


    You may well have got a syntax error -- we can't tell as you posted
    neither the exact code you tried to run, nor the actual error message
    that you got. After correcting any syntax error, one would have
    expected you to get a run-time error, like the following:

    >>> b3 = '\xb6/\x00'
    >>> import struct
    >>> struct.unpack('i', b3)

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


    Luckily this time there was enough extraneous info for Jeff to
    diagnose your problem. In future, please consider posting the code
    that you ran, and the results that you got.

    For the benefit of others who may fall into the same pit, you may like
    to consider logging a feature request on sourceforge for the above
    error message to be enhanced to show the sizes:

    (e.g.) struct.error: unpack str size (3) does not match format (4)

    This may make it easier for the perps to realise what has gone wrong
    and save them from having to ask the newsgroup :)

    Cheers,
    John
    John Machin, May 3, 2005
    #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. nick
    Replies:
    0
    Views:
    892
  2. nick
    Replies:
    1
    Views:
    31,740
    Eric Sosman
    Oct 26, 2004
  3. Robben
    Replies:
    14
    Views:
    559
    Old Wolf
    Dec 27, 2005
  4. Yin Zhu
    Replies:
    15
    Views:
    943
    Richard Bos
    Sep 12, 2007
  5. Vianney Lecroart

    read write integer in binary into a file

    Vianney Lecroart, Oct 25, 2007, in forum: Ruby
    Replies:
    9
    Views:
    124
    Tim Hunter
    Dec 13, 2007
Loading...

Share This Page