Created
April 11, 2011 14:02
-
-
Save jcchurch/913564 to your computer and use it in GitHub Desktop.
This function fails to decipher a listing of binary digits using the ASCII tables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
text = "101011011101010001101101011011101010100010101101010001101100101011011101010001110100111011010101101110110110101110110101000011010101101110110100111011010101101011010100110101001000100100011010100101011100110101001000101011010100110101001010100111010101101001001010101110100101010001010101101001001010100111010101101010101010110001010001110100101011010101010010010100011101000110101010110001010000101001001101010011101011011101010100101011010100111010110100101001001101010011010101000101001010110111010100011111010110110010101101110111010101000101001010111101101010001101001110110101011101101101101011010011101101011101101010001101000101001010111010110101001101010001101001000110101101010011010101001010111010110101001010100010101001110101011010100010101010101010011101011101001010100010101010101110100101101010101100010100010101001010011010101011010110101110100101101010101011" | |
def bin2ascii(binary_form): | |
"""Converts binary strings of length 8 to ASCII""" | |
if len(binary_form) != 8: | |
return "?" | |
return chr(int(binary_form,2)) | |
def group(message, window, shift=-1, skipspaces=True): | |
"""Groups a string message into a list of shorter string messages of length 'window'. By default, the function shifts by size 'window', but that can be set to any value. Also, whitespace characters are skipped.""" | |
if shift == -1: | |
shift = window | |
if skipspaces: | |
message = re.sub("\s+", "", message) | |
tokens = [] | |
i = 0 | |
while i + window <= len(message): | |
tokens += [ message[i:i+window] ] | |
i += shift | |
return tokens | |
def processString(text, removeLead=0): | |
"""First, this function trims the first 'removeLead' characters. Then it groups a string text into clumps of 8 bytes and passes each clump to the binary-to-ascii converter, then prints the message""" | |
text = text[removeLead:] | |
eightbits = group(text, 8) | |
for clump in eightbits: | |
print bin2ascii(clump), | |
if __name__ == '__main__': | |
# Check the list going forward | |
for removeLead in range(8): | |
print removeLead | |
processString(text, removeLead) | |
# Check the list going backward | |
reverse = text[::-1] | |
for removeLead in range(8): | |
print removeLead | |
processString(reverse, removeLead) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment