How to print a file in binary mode

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Oct 22 16:50:10 CEST 2006


In <1161524030.833422.216370 at k70g2000cwa.googlegroups.com>, Lucas wrote:

> thanks for your answer.
> 
> I known how to do it.
> read() return a string. so
> 1) bytes = read(1) #read the file by bit.

This reads a byte, not a bit.

> 2) chrString  = ord(bytes) #convert the string to ASCII.

Converts the byte into an integer with value of the byte.  This has
nothing to do with ASCII.

> 3) print numberToBinary(chrString) #convert the ASCII to Binary using
> my function.

You mean a binary representation i.e. a string that contains just 0s and
1s?

> 4) Loop

This is quite inefficient.  If the file is not too big it's better to load
it into memory completely.  And if you need the byte value of every single
byte you should read the file into an `array.array()` of type 'B'.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list