Skip to content

Instantly share code, notes, and snippets.

@Ge0rg3
Created February 6, 2018 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ge0rg3/0a53a3bc9cb9dc710c0849a6e2159492 to your computer and use it in GitHub Desktop.
Save Ge0rg3/0a53a3bc9cb9dc710c0849a6e2159492 to your computer and use it in GitHub Desktop.
Bin/Dec/Hex Converter
use = True
import os
import time
from sys import platform
def bannerwin():
print(" ")
print(".oPYo. o o ")
print("8 8 8 8 ")
print("8 .oPYo. odYo. o o 8 odYo. o8P ")
print("8 8 8 8' `8 Y. .P 8 8' `8 8 ")
print("8 8 8 8 8 8 `b..d' 8 8 8 8 ")
print("`YooP' `YooP' 8 8 `YP' 8 8 8 8 ")
print(":.....::.....:..::..::...::....::..::..:")
print("::::::::::::::::::::::::::::::::::::::::")
print("::::::::::::::::::::::::::::::::::::::::")
print("ConvInt, found on github.com/georgeomnet\n")
def clearcheck():
if "win" in platform.lower() or platform == "darwin":
print("========================================================\n")
else:
os.system("clear")
def errormessagelater():
print("Sorry! That wasn't a valid answer.")
time.sleep(1)
use = True
clearcheck()
def runden():
usernum = input("Enter your denary number: ")
try:
print("Here are the stats for %s:" % usernum)
print("Its hexadecimal is:",format(int(usernum), '02X'))
print("Its binary is:","{0:b}".format(int(usernum)))
print("Its octal decimal is:",oct(int(usernum)))
except:
print("Sorry, that number is not valid.")
runden()
def runbin():
usernum = input("Enter your binary number: ")
try:
print("Here are the stats for %s:" % usernum)
print("Its denary is:",int(usernum, 2))
print("Its hexadecimal is:",format(int(usernum), '02X'))
print("Its octal decimal is:",oct(int(usernum)))
except:
print("Sorry, that number is not valid.")
runbin()
def runhex():
usernum = input("Enter your hexadecimal number: ")
try:
print("Here are the stats for %s:" % usernum)
print("Its denary is:",int(usernum, 16))
print("Its binary is:",bin(int(usernum, 16))[2:])
print("Its octal decimal is:",oct(int(usernum, 16)))
except:
print("Sorry, that is not a valid hex.")
runhex()
def runoct():
usernum = input("Enter your octal decimal: ")
try:
print("Here are the stats for %s" % usernum)
print("Its denary is:",int(usernum, 8))
print("Its binary is:",bin(int(usernum, 8))[2:])
print("Its hexadecimal is:",format(int(int(usernum, 8)), '02x'))
except:
print("Sorry, that oct is not valid.")
runoct()
def prog():
typeofchar = input("Is your number Denary (d), Binary (b), Hexadecimal (h) or Octal decimal (o)?\n")
typeofchar = typeofchar.lower()
typeofchar = typeofchar[0]
if typeofchar == "d":
runden()
elif typeofchar == "b":
runbin()
elif typeofchar == "h":
runhex()
elif typeofchar == "o":
runoct()
else:
print("Sorry! Your number is not valid.")
while use == True:
bannerwin()
prog()
ask = input("Would you like to go again? y/n\n")
try:
ask = ask[0]
ask = ask.lower()
except:
errormessagelater()
if ask == "n":
use = False
elif ask == "y":
use = True
clearcheck()
else:
errormessagelater()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment