How print binary data on screen

Mensanator mensanator at aol.com
Tue Mar 17 20:34:05 CET 2009


On Mar 17, 1:45 pm, Irmen de Jong <irmen.NOS... at xs4all.nl> wrote:
> Ehsen Siraj wrote:
> > I am trying to print binary data on screen but I got the following error.
>
> > f = open('/home/ehsen/1.mp3','rb')
> > g = f.read()
> > print g
> [...]
> > UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:
> > unexpected code byte
>
> > please help me how i fix this thing.
>
> One way of printing it would be:
>
> print g.encode("hex")
>
> but you don't say what you wanted to accomplish.
> "printing" a mp3 file to the screen doesn't make much sense.

Maybe he's looking for the face of Jesus?

import gmpy

f = open('0010.wav','rb')
g = f.read()

line = []
count = 0
for i in g:
  c = gmpy.digits(ord(i),2).zfill(8)
  line.append(c)
  count += 1
  if count==10:
    print ''.join(line)
    line = []
    count = 0

##01010010010010010100011001000110011001001100100100000000000000000101011101000001
##01010110010001010110011001101101011101000010000000010000000000000000000000000000
##00000001000000000000000100000000001000100101011000000000000000000010001001010110
##00000000000000000000000100000000000010000000000001100100011000010111010001100001
##01000000110010010000000000000000011100110111001101110011011100100111000101110001
##01110001011100100111001001110010011100010111000101110001011100110111001101110011
##01110011011101000111010001110100011100110111010001110100011101100111011001110110
##01111000011110010111101001111011011111000111110001111101011111100111111110000001
##10000001100000101000001110000100100001001000010110000110100010001000100110001011
##10001100100011001000110110001111100100001001000010010001100100101001010010010101
##10010101100101011001011010011000100110001001100110011001100110011001100110011010
##10011011100110101001101010011100100111011001111010011110100111101001111010011110
##10011111100111111001110110011101100111101001111010011101100111011001110010011100
##10011101100111101001110110011101100111011001110110011101100111011001110010011011
##10011011100110101001100110011001100101111001011010010101100101011001001110010010
##10010001100100001000111110001110100011001000100110000111100001011000010010000010
##10000000011111100111101101111001011101110111010101110011011100010110111101101110
##01101011011010000110011101100100011000110110000101100000010111110101110101011100
##01011011010110100101100101011001010110000101011101010111010101010101010101010100
##01010100010100100101000101010010010100100101001001010001010100010101000001010000
##01010000010100000100111101001101010011010100110001001100010011000100101001001010
##01001011010010110100101001001000010010010100101001001010010010110100101001001010
##01001001010010100100101001001010010010100100101101001011010011010100111001001110
##01001111010100100101001101010100010101010101011001010111010110000101101001011011
##01011101010111110110000101100011011001000110010101100111011010010110101101101100
##01101110011011110111000001110001011100110111001101110101011101110111101001111011
## ...continues for thousands of lines

>
> --irmen




More information about the Python-list mailing list