Skip to content

Instantly share code, notes, and snippets.

@dunvi
Last active August 29, 2015 14:10
Show Gist options
  • Save dunvi/2c05a6c16f65c754ab84 to your computer and use it in GitHub Desktop.
Save dunvi/2c05a6c16f65c754ab84 to your computer and use it in GitHub Desktop.
python ascii fun (handy for The Talos Principle)
def numToBinary(N):
if N==0: return ''
else: return numToBinary(N/2)+str(N%2)
def asciiString(S):
rtn = ""
next = ""
for e in S:
next = numToBinary(ord(e))
next = (8-len(next))*'0' + next + ' '
rtn += next
return rtn[:-1] # leave out the final space bc why not
def hexString(S):
rtn = ""
next = ""
for e in S:
rtn += hex(ord(e))[2:] + ' '
return rtn[:-1] # leave out the final space bc why not
def binaryToNum(N):
if N=='': return 0
else: return 2*binaryToNum(N[:-1])+int(N[-1])
def readAscii(S):
rtn = ""
hold = S.split()
for n in hold:
rtn += chr(binaryToNum(n))
return rtn
def readHex(S):
rtn = ""
hold = S.split()
for n in hold:
rtn += chr(int(n, 16))
return rtn
def readBinary(S):
rtn = ""
hold = S.split()
for n in hold:
rtn += chr(binaryToNum(n))
return rtn
@dunvi
Copy link
Author

dunvi commented Dec 8, 2014

live version at http://repl.it/5tE/4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment