Last active
August 29, 2015 14:10
-
-
Save dunvi/2c05a6c16f65c754ab84 to your computer and use it in GitHub Desktop.
python ascii fun (handy for The Talos Principle)
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
live version at http://repl.it/5tE/4