Read binary file and dump data in

S

Santiago Romero

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.
 
A

Albert Hopkins

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
 
C

Chris Rebert

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
 
S

Santiago Romero

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top