Skip to content

Instantly share code, notes, and snippets.

@mos3abof
Last active August 29, 2015 14:03
Show Gist options
  • Save mos3abof/f162ba10576120cc28e3 to your computer and use it in GitHub Desktop.
Save mos3abof/f162ba10576120cc28e3 to your computer and use it in GitHub Desktop.
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