I'm pretty very new to python, so I thought that I'd write a game to teach me python and better teach me how to think in hexadecimal and binary. I'm starting off by writing a few functions to convert between the different bases.
I'm looking for feedback on any better ways to implement these functions. There might be python libraries to do exactly this but I don't care. I can't think of a better way to check for the values A-F, and what they are in different bases, so if there are any better (prettier) ways to do it (I couldn't seem to find a switch/case in python, but maybe I didn't look hard enough).
As well as any possible better implementation, I'd also like input on naming and layout and whatnot.
Hex to binary function:
import re
def hexToBi(hexStr):
hexRegex = "[A-Fa-f0-9]+"
if not bool(re.match(hexRegex, hexStr)):
return ""
biStr = ""
for c in hexStr:
if c == '0':
biStr += "0000 "
elif c == '1':
biStr += "0001 "
elif c == '2':
biStr += "0010 "
elif c == '3':
biStr += "0011 "
elif c == '4':
biStr += "0100 "
elif c == '5':
biStr += "0101 "
elif c == '6':
biStr += "0110 "
elif c == '7':
biStr += "0111 "
elif c == '8':
biStr += "1000 "
elif c == '9':
biStr += "1001 "
elif c.upper() == 'A':
biStr += "1010 "
elif c.upper() == 'B':
biStr += "1011 "
elif c.upper() == 'C':
biStr += "1100 "
elif c.upper() == 'D':
biStr += "1101 "
elif c.upper() == 'E':
biStr += "1110 "
elif c.upper() == 'F':
biStr += "1111 "
return biStr
Binary to Hex function
def biToHex(biStr):
biSpRegex = "^([01]{4}[\s^\s])+$"
biRegex = "^([01]{4})+$"
if not bool(re.match(biSpRegex, biStr)):
biStr = biStr.replace(" ", "")
if not bool(re.match(biRegex, biStr)):
return ""
nibbles = [biStr[i:i+4] for i in range(0, len(biStr), 4)]
hexStr = ""
for s in nibbles:
val = 0
if s[0] == '1':
val = 8
if s[1] == '1':
val += 4
if s[2] == '1':
val += 2
if s[3] == '1':
val += 1
if val < 10:
hexStr += str(val)
elif val == 10:
hexStr += 'A'
elif val == 11:
hexStr += 'B'
elif val == 12:
hexStr += 'C'
elif val == 13:
hexStr += 'D'
elif val == 14:
hexStr += 'E'
elif val == 15:
hexStr += 'F'
return hexStr
Binary and Hex to decimal functions
def hexToDec(hexStr):
hexRegex = "[A-Fa-f0-9]+"
if not bool(re.match(hexRegex, hexStr)):
return ""
val = 0;
power = len(hexStr) - 1
for i in range(0, len(hexStr)):
valAt = 0
if hexStr[i].upper() == 'A':
valAt = 10
elif hexStr[i].upper() == 'B':
valAt = 11
elif hexStr[i].upper() == 'C':
valAt = 12
elif hexStr[i].upper() == 'D':
valAt = 13
elif hexStr[i].upper() == 'E':
valAt = 14
elif hexStr[i].upper() == 'F':
valAt = 15
elif int(hexStr[i]) < 10:
valAt = int(hexStr[i])
valAt = valAt * (16**power)
power -= 1
val += valAt
return val
def biToDec(biStr):
return hexToDec(biToHex(biStr))
very quick and limited set of unit tests
def unitTest():
assert (hexToBi("ABC09") == "1010 1011 1100 0000 1001"), "Logic fail, \"1010 1011 1100 0000 1001\" expected, " + hexToBi("ABC09") + " recieved"
assert (hexToBi("QWER") == ""), "Logic fail, \"\" expected, " + hexToBi("QWER") + " recieved"
assert (biToHex("1111 1111 0000") == "FF0"), "Logic fail, \"FF0\" expected, " + biToHex("1111 1111 0000") + " recieved"
assert (biToHex("1101 0000") == "D0"), "Logic fail, \"D0\" expected, " + biToHex("1101 0000") + " recieved"
assert (hexToDec("FAC") == 4012), "Logic fail, \"4012\" expected, " + hexToDec("FAC") + " recieved"
assert (biToDec("1101 0111") == 215), "Logic fail, \"215\" expected, " + biToDec("1101 0111") == 215 + " recieved"