Hexadecimals to decimals
Similar to the previous number systems with less than 10 digits, but this time there's more than 10. For this, the same thing happens, but you need to define the digits bigger than 10. For hexadecimals, that would be: a(10), b(11), c(12), d(13), e(14) and f(15). This time, I put the separate digits into the indexes of an array, and every time one of a, b, c, d, e or f came up, I instead inserted the corresponding numbers:
def hexadecimal(number):
numb_array = []
for x in range(0, len(number)):
if number[x] == "A" or number[x] == "a":
numb_array.append(10)
elif number[x] == "B" or number[x] == "b":
numb_array.append(11)
elif number[x] == "C" or number[x] == "c":
numb_array.append(12)
elif number[x] == "D" or number[x] == "d":
numb_array.append(13)
elif number[x] == "E" or number[x] == "e":
numb_array.append(14)
elif number[x] == "F" or number[x] == "f":
numb_array.append(15)
else:
numb_array.append(int(number[x]))
numb_array.reverse()
output = 0
for x in range(0, len(numb_array)):
output += int(numb_array[x]) * (16 ** x)
return output
If I wanted to do this with any other number system with more than 10 digits, all I will have to do is define the digits, and then change the 16 at the bottom to the number of digits.
Comments
Post a Comment