Read binary file and dump data in

Discussion in 'Python' started by Santiago Romero, Jan 13, 2009.

  1. Hi.

    Until now, all my python programs worked with text files. But now I'm
    porting an small old C program I wrote lot of years ago to python and
    I'm having problems with datatypes (I think).

    some C code:

    fp = fopen( file, "rb");
    while !feof(fp)
    {
    value = fgetc(fp);
    printf("%d", value );
    }

    I started writing:

    fp = open(file, "rb")
    data = fp.read()
    for i in data:
    print "%d, " % (int(i))

    But it complains about i not being an integer... . len(data) shows
    exactly the file size, so maybe is a "type cast" problem... :-?

    What's the right way to work with the binary data (read 1 byte values
    and work with them, dumping them as an integer in this case)?

    Thanks.
    Santiago Romero, Jan 13, 2009
    #1
    1. Advertising

  2. On Tue, 2009-01-13 at 12:02 -0800, Santiago Romero wrote:
    > Hi.
    >
    > Until now, all my python programs worked with text files. But now I'm
    > porting an small old C program I wrote lot of years ago to python and
    > I'm having problems with datatypes (I think).
    >
    > some C code:
    >
    > fp = fopen( file, "rb");
    > while !feof(fp)
    > {
    > value = fgetc(fp);
    > printf("%d", value );
    > }
    >
    > I started writing:
    >
    > fp = open(file, "rb")
    > data = fp.read()
    > for i in data:
    > print "%d, " % (int(i))
    >
    > But it complains about i not being an integer... . len(data) shows
    > exactly the file size, so maybe is a "type cast" problem... :-?
    >


    int() expects something that "looks like" an integer. E.g.

    int(2) => 2
    int(2.0) => 2
    int('2') => 2
    int('c') => ValueError

    If you are reading arbitrary bytes then it will likely not always "look"
    like integers. What you probably meant is:

    for i in data:
    print "%d, " % ord(i)

    But if you are really dealing with C-like data structures then you might
    be better off using the struct module.

    -a
    Albert Hopkins, Jan 13, 2009
    #2
    1. Advertising

  3. Santiago Romero

    Chris Rebert Guest

    On Tue, Jan 13, 2009 at 12:02 PM, Santiago Romero <> wrote:
    >
    > Hi.
    >
    > Until now, all my python programs worked with text files. But now I'm
    > porting an small old C program I wrote lot of years ago to python and
    > I'm having problems with datatypes (I think).
    >
    > some C code:
    >
    > fp = fopen( file, "rb");
    > while !feof(fp)
    > {
    > value = fgetc(fp);
    > printf("%d", value );
    > }
    >
    > I started writing:
    >
    > fp = open(file, "rb")
    > data = fp.read()
    > for i in data:
    > print "%d, " % (int(i))
    >
    > But it complains about i not being an integer... . len(data) shows
    > exactly the file size, so maybe is a "type cast" problem... :-?
    >
    > What's the right way to work with the binary data (read 1 byte values
    > and work with them, dumping them as an integer in this case)?


    Albert already pointed out the problem with using int(), so I'll just
    say that you might be interested in the `struct` module:
    http://docs.python.org/library/struct.html

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
    Chris Rebert, Jan 13, 2009
    #3

  4. > If you are reading arbitrary bytes then it will likely not always "look"
    > like integers. What you probably meant is:
    >
    > for i in data:
    >    print "%d, " % ord(i)


    That's it! :)

    Thanks a lot.
    Santiago Romero, Jan 14, 2009
    #4
    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. Pete Fraser
    Replies:
    4
    Views:
    6,765
    Mike Treseler
    Nov 4, 2004
  2. Doug
    Replies:
    3
    Views:
    4,349
    Bruce Barker
    Nov 4, 2005
  3. John Adams

    read binary data from C file???

    John Adams, Sep 22, 2004, in forum: Java
    Replies:
    15
    Views:
    6,437
    Steve Horsley
    Sep 24, 2004
  4. halfdog
    Replies:
    12
    Views:
    12,409
  5. Bike Rider

    Binary dump in browser

    Bike Rider, May 20, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    91
    A. Sinan Unur
    May 20, 2004
Loading...

Share This Page