Skip to content

Instantly share code, notes, and snippets.

@eduardoedson
Last active July 19, 2016 20:37
Show Gist options
  • Save eduardoedson/d3e659cf02761c0074f56db5de337a7b to your computer and use it in GitHub Desktop.
Save eduardoedson/d3e659cf02761c0074f56db5de337a7b to your computer and use it in GitHub Desktop.
ASCII to Binary / Binary to ASCII
# -*- coding: utf-8 -*-
import binascii
import os
def to_binary(frase):
return bin(int(binascii.hexlify(frase), 16))
def to_ascii(frase):
return binascii.unhexlify('%x' % int(frase, 2))
# ======================================================================
function = raw_input('[1] - Passar para ASCII\n[2] - Passar para Binário\n')
frase = raw_input('\nQual a frase?\n')
os.system('clear')
if int(function) == 1:
print('Frase em Binário: ' + frase)
print('Frase em ASCII: ' + to_ascii(frase))
elif int(function) == 2:
print('Frase em ASCII: ' + frase)
print('Frase em Binário: ' + to_binary(frase))
else:
print('Opção inválida')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment