Python code for converting hexadecimal to decimal numbers And Check whether given hex number is equal to
provided decimal number:
# Make a dictionary of Hexadecimal to decimal conversion table
HexToDecimalConvTable = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'A': 10 , 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15}
# Enter some hexadecimal number (strip remmove space and upper convert into Capital Latter)
hexadecimalNumber = input("Enter the hexadecimal number: ").strip().upper()
#Enter the provided decimal number
decimalNumber=int(input("Enter provided decimal number: "))
decimal = 0
#computing max power value
power = len(hexadecimalNumber) -1
for digit in hexadecimalNumber:
decimal += HexToDecimalConvTable[digit]*16**power
power -= 1
print(decimal)
# decimal is calculated decimal number
if(decimalNumber == decimal):
print("Given Hexadecimal is equal to decimal number")
else:
print("Hexadecimal number is not match with decimal")
#Input is FF
#Output is 255
The standard mathematical way to convert hexadecimal to decimal is to multiply each digit of hexadecimal number with corresponding power of 16 and sum it.
Like: decimal += HexToDecimalConvTable[digit]*16**power
0 Comments