Yeah, that's what I thought. But this is the code I have - it does not seem to work, just prints out a blank line in the shell then RESTART. And then the whole shell/IDLE seemed to crash when I closed them.
The file I'm trying to read one bit at a time from is an encrypted jpeg. This is the code I have:
encrypted = 
fh = open('encjpgfile', 'rb')
bit = fh.read(1)
from binascii import hexlify
L = ["00", "01", "10", "11"]
LL = [u+v for u in L for v in L]
s = "0123456789abcdef"
D = dict((s[i], LL[i]) for i in range(16))
jpeg = open('encjpgfile', 'rb').read()
bits = ''.join(D[x] for x in hexlify(jpeg))
# can be used for Python25/26
def int2bin(num, bits):
returns the binary of integer num, using bits
number of digits, will pad with leading zeroes
bs = ''
for x in range(0, bits):
if 2**x == 2**x & num:
bs = '1' + bs
bs = '0' + bs
image_file = "red.jpg" # a testfile
# read all data into a string
data = open(image_file, 'rb').read()
print "Image file %s not found" % image_file
bit_list = 
# create a list padded bits
for ch in data:
# take the int value of ch and convert to 8 bit strings
bits8 = int2bin(ord(ch), 8)
my result -->
['11111111', '11011000', '11111111', '11100000', '00000000', ... ]
that works great, thank you! Only thing is, when I print out the 'encrypted' list, it's full of characters like '\x0e', '\xc4' etc etc... not the 1s and 0s I want, how do I get it to do this?
Sorry for all the questions, very limited knowledge I have, you've been very helpful though.
Yes, unfortunately Python's file handling wasn't intended for bit-by-bit reading of a file; the read function takes an optional parameter for the number of bytes to read. Since we used '1', that told Python to read the file byte-by-byte. Each of the characters that you see is a single byte of the image data.
Equipped with the knowledge that a byte consists of 8 bits, you could consider it a personal challenge to come up with a way to take each of those bytes and translate it into the corresponding bits.
\xc4 means hexadecimal C4, a.k.a. 0xC4
In fact, I believe that Python 3.0 (and therefore 2.6) has a built-in method bin() that would translate directly to binary.
Just one more question - if I have modified each of the 8-bit elements in the array and therefore created a new array of bit patterns, how can I then print the data from such an array (as binary data) to a file?
Let's say you have a list of your 8-bit binary elements called new_bytes , if you already have them back into '\x00' form, you could simply write to file like this:
fh = open( 'my_new_file.jpg', 'wb' )
for new_byte in new_bytes:
However if you still need to convert the binary string into hex then you can do either use something like hex('0b10011010') (NOTE: the notation 0b denotes Binary, just as 0x denotes hex-refer here for more details).