Skip to content

Instantly share code, notes, and snippets.

@adamwespiser
Created May 23, 2015 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamwespiser/f3be29b668cfd3ed8598 to your computer and use it in GitHub Desktop.
Save adamwespiser/f3be29b668cfd3ed8598 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import string
import sys
print "NOT WORKING YET, no decode"
# text(limited set) -> binary representation -> base 8 representation -> exctact 0-88 pairs, add 32, get ascii code
sys.exit(0)
def getCharacters():
chars = string.ascii_lowercase
punc = ['.', ',', '!', '?', '<END>']
return [ c for c in chars ] + punc
def getLetter(bits):
try:
result = string.ascii_lowercase[int(bits,2)]
except Exception as e:
print e
result = "?"
finally:
return result
def getBits(letter):
alphabet = getCharacters()
try:
result = alphabet.index(token)
except Exception as e:
print e
result = -1
finally:
return result
# http://stackoverflow.com/questions/8898807/pythonic-way-to-iterate-over-bits-of-integer
def bits(number):
mask = 2**5 - 1
while number:
res = mask & number
yield res
number = number >> 5
def chunks(l, n=2):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def textToBinary(text):
alphabet = getCharacters()
col = []
shift = 0
for token in text.lower():
num = 30 # ' coded as '?'
if token in alphabet:
num = alphabet.index(token) + 1
num = num << shift
shift = shift + 5
col.append(num)
#col.append(31 << shift)
return sum(col)
def numberToAscii(number):
chars = str(number)
chars = chars.replace('L','')
col = ""
if (len(chars) % 2 == 1):
chars = '0' + chars
for x in chunks(chars):
number = int(x) + 33
letter = str(unichr(number))
print x + ": " + str(number) + " " + letter
col += letter
return col
numberToAscii(oct(textToNumber("hello, how are you")))
decode: ascii -> octal -> bit -> character map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment