I wanted to create a Python program that takes binary input and converts them into a different format. So, here we go. The main.py is pretty straightforward, it just imports and calls the functions, presented by a series of options, regarding the intended action.
import base64
import binascii
import base
import ascbin
import binoctal
import bindecimal
import binhexadecimal
print("Welcome to prcrypt!")
print("Please choose one of the following options:")
print("Choice 1: Base64:")
print("Choice 2: Binary - Text:")
print("Choice 3: Binary - Octal:")
print("Choice 4: Binary - Decimal:")
print("Choice 5: Binary - Hexadecimal:")
print("Choice 0: Terminate the program:")
choice = input()
if (choice == "0"):
print("Ending...")
elif (choice == "1"):
base.base64_function()
elif (choice == "2"):
ascbin.ascbin_function()
elif (choice == "3"):
binoctal.binoctal_function()
elif (choice == "4"):
bindecimal.bindecimal_function()
elif (choice == "5"):
binhexadecimal.binhex_function()
else:
print("Invalid Choice...")
print("Ending program...")
Letβs proceed with the base function. First, it takes a text as input encodes it into the ASCII representation, then encode it into b64 format, and finally decodes it again into ASCII in order to print it. The base64 decoding operates on the opposite logic.
import base64
def base64_function():
print("You choose Base64.")
print("Please type if you want to 'encode' or 'decode...")
encdec = input()
# base64 encoding
if (encdec == "encode"):
print("You choose Encode...")
print("Please type the text you want to encode...")
base64txt = input()
base64txt_byte = base64txt.encode("ascii")
base64_byte = base64.b64encode(base64txt_byte)
base64_final = base64_byte.decode("ascii")
print("Result: " + base64_final)
# base64 decoding
if (encdec == "decode"):
print("You choose Decode...")
print("Please type the text you want to decode...")
base64txt = input()
base64txt_byte = base64txt.encode("ascii")
base64_byte = base64.b64decode(base64txt_byte)
base64_final = base64_byte.decode("ascii")
print("Result: " + base64_final)
Next, it is the turn of conversion from text to binary and vice-versa. We are using ord() which returns the number representing the Unicode code of a character. For the conversion from binary to text, we first split the input with the help of for looping. Finally, we convert it to a binary value with the help of bin, and afterward, we use chr() in order to return the specified Unicode.
def ascbin_function():
print("You choose Binary - Text.")
print("Please type 'text' if you want to convert from 'text' to 'binary':")
print("Please type 'binary' if you want to convert from 'binary' to 'text':")
binary_input = input()
if (binary_input == "text"):
print("Give the text you want to convert...")
txt = input()
txt_bin = ' '.join(format(ord(i), 'b') for i in txt)
print("Result: " + txt_bin)
if (binary_input == "binary"):
print("Give the binary you want to convert...")
txt = input()
bin_values = txt.split()
ascii_str = ""
for bin_value in bin_values:
integer_val = int(bin_value, 2)
ascii_char = chr(integer_val)
ascii_str = ascii_str + ascii_char
print(ascii_str)
Continuing, we move to binary-octal conversion. First, we take as an input a binary value, and with the help of oct() and [2:], we convert it to an octal representation, removing at the same time the 0b. The exact same process we follow with the opposite process, but using the bin command.
def binoctal_function():
print("You choose Binary - Octal.")
print("Please type 'binary' if you want to convert from 'binary' to 'octal':")
print("Please type 'octal' if you want to convert from 'binary' to 'binary':")
binary_input = input()
if (binary_input == "binary"):
print("Give the binary value you want to convert...")
txt = input()
octal = int(txt, 2)
octal = oct(octal)
print(octal[2:])
if (binary_input == "octal"):
print("Give the octal value you want to convert...")
txt = input()
bin_txt = int(txt, 8)
bin_txt = bin(bin_txt)
print(bin_txt[2:])
We have not much to say here. We convert it to an integer with int() from binary and to binary with the help of bin() and [2:] to remove β0bβ.
def bindecimal_function():
print("You choose Binary - Decimal.")
print("Please type 'binary' if you want to convert from 'binary' to 'decimal':")
print("Please type 'decimal' if you want to convert from 'decimal' to 'binary':")
binary_input = input()
if (binary_input == "binary"):
print("Give the binary value you want to convert...")
txt = input()
bin_txt = int(txt, 2)
print(bin_txt)
if (binary_input == "decimal"):
print("Give the decimal value you want to convert...")
txt = input()
dec_txt = bin(int(txt))
dec_txt = dec_txt[2:]
print(dec_txt)
In order to convert from binary to hex we follow this procedure. First, we use hex() and int() to convert them to hex and to an integer. Afterward, hex has a base of 16, and with bin() and [2:] we convert it to binary removing at the same time the β0bβ.
import binascii
def binhex_function():
print("You choose Binary - Hexadecimal.")
print("Please type 'binary' if you want to convert from 'binary' to 'hex':")
print("Please type 'hex' if you want to convert from 'hex' to 'binary':")
binary_input = input()
if (binary_input == "binary"):
print("Give the binary value you want to convert...")
txt = input()
hex_txt = hex(int(txt, 2))
hex_txt = hex_txt[2:]
print(hex_txt.upper())
if (binary_input == "hex"):
print("Give the hex value you want to convert...")
txt = input()
bin_txt = bin(int(txt, 16))[2:]
print(bin_txt)
git clone https://github.com/mariosdaskalas/pcrypt
cd pcrypt/
python3 main.py
Or a one-liner command.
git clone https://github.com/mariosdaskalas/pcrypt && cd pcrypt/ && python3 main.py
You can find the Source Code on Github.
Image by Gerd Altmann from Pixabay
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
Comments