424,852 Members | 888 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

# How to print a file in binary mode

 P: n/a 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! Oct 22 '06 #1
8 Replies

 P: n/a In <11**********************@m73g2000cwd.googlegroups .com>, 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 Oct 22 '06 #2

 P: n/a 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 <11**********************@m73g2000cwd.googlegroups .com>, 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 Oct 22 '06 #3

 P: n/a In <11**********************@k70g2000cwa.googlegroups .com>, 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 Oct 22 '06 #4

 P: n/a 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? Oct 22 '06 #5

 P: n/a 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? Oct 22 '06 #6

 P: n/a 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 ? Oct 22 '06 #7