Skip to content

Instantly share code, notes, and snippets.

@zst123
Created May 1, 2017 03:14
Show Gist options
  • Save zst123/d2d41316d5e7b514d1b5750425012083 to your computer and use it in GitHub Desktop.
Save zst123/d2d41316d5e7b514d1b5750425012083 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import os
class CmdColors:
ATTR_OFF = '\033[0m'
DEFAULT = '\033[99m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# Colors
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
PINK = '\033[95m'
CYAN = '\033[96m'
WHITE = '\033[97m'
# Light colors
LIGHT_RED = '\033[31m'
LIGHT_GREEN = '\033[32m'
BROWN = '\033[33m'
LIGHT_BLUE = '\033[34m'
PALE_CYAN = '\033[36m'
GREY = '\033[37m'
# Highlights
HIGHLIGHT_WHITE_ON_BLACK = '\033[7m'
HIGHLIGHT_RED = '\033[101m'
HIGHLIGHT_GREEN = '\033[102m'
HIGHLIGHT_YELLOW = '\033[103m'
HIGHLIGHT_BLUE = '\033[104m'
HIGHLIGHT_PINK = '\033[105m'
HIGHLIGHT_CYAN = '\033[106m'
HIGHLIGHT_WHITE = '\033[107m'
def parseInput(val):
if val.startswith('q'):
print "Exiting"
sys.exit()
numb = parseHex(val)
if numb is not None:
print CmdColors.WHITE + "Hexadecimal input = %s" % val
return numb
numb = parseBin(val)
if numb is not None:
print CmdColors.YELLOW + "Binary input = %s" % val
return numb
numb = parseOct(val)
if numb is not None:
print CmdColors.GREEN + "Octal input = %s" % val
return numb
print CmdColors.CYAN + "Decimal input = %s" % val
return parseDec(val)
def parseHex(val):
if val.startswith("0x"):
return int(val, 16)
if val.startswith("#") or val.startswith("h"):
return int(val[1:], 16)
if val.endswith("h"):
return int(val[:-1], 16)
if val.endswith("hex"):
return int(val[:-3], 16)
return None
def parseOct(val):
if val.startswith("0o"):
return int(val, 8)
if val.startswith("0") or val.startswith("o"):
if val.isdigit() and ('8' not in val) and ('9' not in val):
return int(val[1:], 8)
if val.endswith("o"):
return int(val[:-1], 8)
if val.endswith("oct"):
return int(val[:-3], 8)
return None
def parseBin(val):
if val.startswith("0b"):
return int(val, 2)
if val.startswith("b"):
return int(val[1:], 2)
if val.endswith("b"):
return int(val[:-1], 2)
if val.endswith("bin"):
return int(val[:-3], 2)
if len(val) >= 4:
x = set(val)
if len(x) == 2 and ('1' in x) and ('0' in x):
return int(val, 2)
return None
def parseDec(val):
if val.startswith("0d"):
val = val[2:]
if val.startswith("d"):
val = val[1:]
if val.endswith("d"):
val = val[:-1]
if val.endswith("dec"):
val = val[:-3]
return int(val)
def addCharEveryN(s, n, c):
return c.join(s[i:i+n] for i in range(0, len(s), n))
while True:
val = raw_input(CmdColors.ATTR_OFF + ">> ")
val = val.strip().lower().replace(" ", "").replace("\xc2", "").replace("\xa0", "")
os.system('clear')
try:
out = parseInput(val)
print " "
print CmdColors.CYAN + "Dec = %d" % out
print CmdColors.WHITE + "Hex = %s" % hex(out).upper().replace("0X", "0x")
print CmdColors.YELLOW + "Bin = %s" % bin(out)
print CmdColors.YELLOW + "Bin = %s" % addCharEveryN('{0:08b}'.format(out), 4, " ")
print CmdColors.GREEN + "Oct = %s" % oct(out)
print CmdColors.ATTR_OFF + " "
print " "
except ValueError:
print CmdColors.ATTR_OFF + " "
print "Invalid input"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment