How to print a file in binary mode

L

Lucas

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!
 
M

Marc 'BlackJack' Rintsch

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
 
L

Lucas

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 : )
 
M

Marc 'BlackJack' Rintsch

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?

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
 
F

Fredrik Lundh

Lucas said:
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>
 
L

Lucas

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
 
F

Fredrik Lundh

Lucas said:
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":
660

what do you want it to print ?

</F>
 
L

Lucas

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top