Last active
August 29, 2015 14:03
-
-
Save mos3abof/f162ba10576120cc28e3 to your computer and use it in GitHub Desktop.
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 ascii_to_binary(ascii_text): | |
binary_text = '' | |
for c in ascii_text: | |
binary_text += bin(ord(c))[2:].zfill(8) | |
return binary_text | |
def binary_to_ascii(binary_text): | |
return ''.join(chr(int(binary_text[i:i+8], 2)) for i in xrange(0, len(binary_text), 8)) | |
if __name__ == '__main__': | |
my_ascii = "Hello, world!" | |
my_binary = ascii_to_binary(my_ascii) | |
print "The binary of '{}' is '{}'".format(my_ascii, my_binary) | |
binary_text = '010001110110010101100101011010110111001100001101' | |
ascii_text = binary_to_ascii(binary_text) | |
print "The reverse ascii of '{}' is '{}'".format(binary_text, ascii_text) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment