Print a string in binary format

Bengt Richter bokr at oz.net
Fri Jan 21 00:23:24 EST 2005


On 20 Jan 2005 17:21:42 -0800, "neutrino" <hanchunhui at gmail.com> wrote:

>Mmm, the output format that I want is like:
>0001110011111111000000001111111100000000....
>
>I tried your code on Cygwin under Windows 2000, and on Linux, but it
>prints out ASCII characters.
>

You can make a 256-length list to translate 0..255 byte codes into binary strings thus:

 >>> byte2bits = [''.join(['01'[i&(1<<b)>0] for b in xrange(7,-1,-1)]) for i in xrange(256)]

Then you can look up string representations for bytes thus:

 >>> byte2bits[10]
 '00001010'
 >>> byte2bits[255]
 '11111111'

and if you had a byte (single character string) that was part of a binary file buffer
that you had read whose value was e.g., '\xa3' you could get its representation thus:

 >>> byte2bits[ord('\xa3')]
 '10100011'

and then you can make a generator routine to convert and join little sequences of bytes, e.g.,

 >>> def bytes2bin(byte_chunk_seq, bspacer=' '):
 ...     for byte_chunk in byte_chunk_seq:
 ...         yield bspacer.join([byte2bits[ord(byte)] for byte in byte_chunk])
 ...
 >>> bytes2bin(['\x00\x01', '\xa3\xa4\xa5\xa6', '@ABCDEFG'])
 <generator object at 0x02EF452C>
 >>> for line in bytes2bin(['\x00\x01', '\xa3\xa4\xa5\xa6', '@ABCDEFG']): print line
 ...
 00000000 00000001
 10100011 10100100 10100101 10100110
 01000000 01000001 01000010 01000011 01000100 01000101 01000110 01000111

If you want to feed bytes2bin with a sequence of 8-byte chunks from a binary file,
you can define a chunk sequence thus (the last chunk might not be a full 8 bytes):

    chunkseq = iter(lambda f=open(filename, 'rb'):f.read(8), '')

and then you can use that as above to print a binary file's content. E.g.,

First we'll make a quick test file

 >>> open('btest.bin', 'wb').write(''.join([chr(i&0xff) for i in xrange(384)]))

Then we'll read, convert and print:

 >>> chunkseq = iter(lambda f=open('btest.bin','rb'): f.read(8), '')
 >>> for line in bytes2bin(chunkseq): print line
 ...
 00000000 00000001 00000010 00000011 00000100 00000101 00000110 00000111
 00001000 00001001 00001010 00001011 00001100 00001101 00001110 00001111
 00010000 00010001 00010010 00010011 00010100 00010101 00010110 00010111
 00011000 00011001 00011010 00011011 00011100 00011101 00011110 00011111
[...snip stuff you can infer ...]
 11100000 11100001 11100010 11100011 11100100 11100101 11100110 11100111
 11101000 11101001 11101010 11101011 11101100 11101101 11101110 11101111
 11110000 11110001 11110010 11110011 11110100 11110101 11110110 11110111
 11111000 11111001 11111010 11111011 11111100 11111101 11111110 11111111
 00000000 00000001 00000010 00000011 00000100 00000101 00000110 00000111
 00001000 00001001 00001010 00001011 00001100 00001101 00001110 00001111
 00010000 00010001 00010010 00010011 00010100 00010101 00010110 00010111
[...snip stuff you can infer ...]
 01110000 01110001 01110010 01110011 01110100 01110101 01110110 01110111
 01111000 01111001 01111010 01111011 01111100 01111101 01111110 01111111

Well, you get the idea.

Regards,
Bengt Richter



More information about the Python-list mailing list