Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

How to print a file in binary mode

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


Luwian at gmail

Oct 22, 2006, 5:16 AM

Post #1 of 10 (438 views)
Permalink
How to print a file in binary mode

I need print a file in binary mode .

f = f.open('python.jpg','rb')
bytes = f.read()
f.close()

print(bytes)

I can't get any binary code.

how to do it?

Thank you very much!

--
http://mail.python.org/mailman/listinfo/python-list


bj_666 at gmx

Oct 22, 2006, 6:06 AM

Post #2 of 10 (420 views)
Permalink
Re: How to print a file in binary mode [In reply to]

In <1161519385.614985.311240 [at] m73g2000cwd>, Lucas wrote:

> I need print a file in binary mode .
>
> f = f.open('python.jpg','rb')
> bytes = f.read()
> f.close()
>
> print(bytes)
>
> I can't get any binary code.

What do you mean by "binary code"? If you use ``print repr(bytes)``
everything outside ASCII will be printed as escape sequence.

But why do you want to "print" JPEG images anyway?

Ciao,
Marc 'BlackJack' Rintsch

--
http://mail.python.org/mailman/listinfo/python-list


Luwian at gmail

Oct 22, 2006, 6:33 AM

Post #3 of 10 (420 views)
Permalink
Re: How to print a file in binary mode [In reply to]

thanks for your answer.

I known how to do it.
read() return a string. so
1) bytes = read(1) #read the file by bit.
2) chrString = ord(bytes) #convert the string to ASCII.
3) print numberToBinary(chrString) #convert the ASCII to Binary using
my function.
4) Loop

I do it because I want to encrypt a string into a picture using RSA
algorithm.
so I first convert the string to binary,and then saving the binary into
picture

finally, print the picture by binary!

It is my coursework and studying PYTHON passingly : )

Marc 'BlackJack' Rintsch wrote:
> In <1161519385.614985.311240 [at] m73g2000cwd>, Lucas wrote:
>
> > I need print a file in binary mode .
> >
> > f = f.open('python.jpg','rb')
> > bytes = f.read()
> > f.close()
> >
> > print(bytes)
> >
> > I can't get any binary code.
>
> What do you mean by "binary code"? If you use ``print repr(bytes)``
> everything outside ASCII will be printed as escape sequence.
>
> But why do you want to "print" JPEG images anyway?
>
> Ciao,
> Marc 'BlackJack' Rintsch

--
http://mail.python.org/mailman/listinfo/python-list


felipe.lessa at gmail

Oct 22, 2006, 6:58 AM

Post #4 of 10 (422 views)
Permalink
Re: How to print a file in binary mode [In reply to]

22 Oct 2006 06:33:50 -0700, Lucas <Luwian [at] gmail>:

> I known how to do it.
> read() return a string. so
> 1) bytes = read(1) #read the file by bit.
> 2) chrString = ord(bytes) #convert the string to ASCII.
> 3) print numberToBinary(chrString) #convert the ASCII to Binary using
> my function.
> 4) Loop


[numberToBinary(ord(x)) for x in f.read()] ?

--
Felipe.


bj_666 at gmx

Oct 22, 2006, 7:50 AM

Post #5 of 10 (422 views)
Permalink
Re: How to print a file in binary mode [In reply to]

In <1161524030.833422.216370 [at] k70g2000cwa>, 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
--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Oct 22, 2006, 7:55 AM

Post #6 of 10 (425 views)
Permalink
Re: How to print a file in binary mode [In reply to]

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>

--
http://mail.python.org/mailman/listinfo/python-list


Luwian at gmail

Oct 22, 2006, 9:09 AM

Post #7 of 10 (426 views)
Permalink
Re: How to print a file in binary mode [In reply to]

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>

--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Oct 22, 2006, 9:21 AM

Post #8 of 10 (424 views)
Permalink
Re: How to print a file in binary mode [In reply to]

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.

I'm afraid that the more you write, the less sense you make.

> do these?
>
> fileWrite = open('a.jpg','ab')
> fileWrite.write('660')
> fileWrite.close()
>
> fileRead = open('a.jpg','rb')
> b = fileRead.read()
> fileRead.close()
> print b

that prints three bytes, "6", "6", and "0":

>>> fileWrite = open('a.jpg','ab')
>>> fileWrite.write('660')
>>> fileWrite.close()

>>> fileRead = open('a.jpg','rb')
>>> b = fileRead.read()
>>> fileRead.close()
>>> print b
660

what do you want it to print ?

</F>

--
http://mail.python.org/mailman/listinfo/python-list


Luwian at gmail

Oct 22, 2006, 9:29 AM

Post #9 of 10 (422 views)
Permalink
Re: How to print a file in binary mode [In reply to]

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>

--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Oct 22, 2006, 11:04 AM

Post #10 of 10 (422 views)
Permalink
Re: How to print a file in binary mode [In reply to]

Lucas wrote:

> # How can I display a.jpg's binary code?

looks like you're confusing binary numbers with binary files:

http://en.wikipedia.org/wiki/Binary_numeral_system
http://en.wikipedia.org/wiki/Binary_file

you don't really need the former to encrypt the contents of the file;
algorithms tend to work just fine no matter what number system you use
to represent the input and output data.

</F>

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.