[Tutor] binary, ascii, steganography

Steven D'Aprano steve at pearwood.info
Fri Mar 18 12:16:39 CET 2011

jaco erasmus wrote:

> Baby steps, first, though.  For the moment, I just want to compare 2 text
> files, have similar letters give me 0 and different letters give me 1.  I
> want to take those and turn them into letters.  How would I do that?

The basic algorithm is:

1 Read one file into text1
2 Read second file into text2
3 Iterate over both together, taking one character from each
4 If first character == second character then bit = 0
   else bit = 1
5 Collect 8 bits and put them together to a number between 0 and 255
6 Convert that number to a character

In Python, here's an outline:

text1 = open('file1').read()
text2 = open('file2').read()
characters = []
bits = []
for c1, c2 in zip(text1, text2):
     if c1 == c2:
     if len(bits) == 8:
         bits = []
print ''.join(characters)

The tricky bit is the function bits_to_character(), which should take a 
list of eight numbers 0 or 1, and return a character. Try writing that, 
and email back if you have trouble.


More information about the Tutor mailing list