How to print a file in binary mode

Lucas Luwian at gmail.com
Sun Oct 22 12:29:33 EDT 2006


I am sorry my english is not good!

strList = ['1010010100','11101000100']

fileWrite = open('a.jpg','ab')
for item in strList:
       fileWrite.write(item)
fileWrite.close()
# I don't know whether or not I succeed

fileRead = open('a.jpg','rb')
b = fileRead.read()
fileRead.close()
print b
#it is wrong
# How can I display a.jpg's binary code?

Lucas wrote:
> well,  if I just open the file in binary mode, and write a string,e.g
> '660', how do i read the result? I means I need print the binary in
> screen.
>
> do these?
>
> fileWrite = open('a.jpg',''ab')
> fileWrite.write('660')
> fileWrite.close()
>
> fileRead = open('a.jpg','rb')
> b = fileRead.read()
> fileRead.close()
> print b
>
> ???????
>
> Thanks again
>
> Fredrik Lundh wrote:
> > Lucas wrote:
> >
> > > I do it because I want to encrypt a string into a picture using RSA
> > > algorithm.
> >
> > what does "into" mean?  are you supposed to encrypt the binary data
> > representing the JPEG image, or embed a message into the actual image?
> >
> >  > so I first convert the string to binary,and then saving the binary
> >  > into picture
> >
> > you seem to have a rather fuzzy understanding of the words "string" and
> > "binary" here.  if you read from a file that's opened in binary mode,
> > you get an 8-bit string that contains the binary data.  there's no need
> > for any conversion here; just use the data you got from "read".
> >
> >  > finally, print the picture by binary!
> >
> > do you mean "save the picture to a binary file", or something else?
> > 
> > </F>




More information about the Python-list mailing list