Hexadecimal to Decimal in Python | The hexadecimal number system is a numeral system made up of 16 symbols. The hexadecimal number system is also known as hex. This system uses the decimal numbers (base 10) and six extra symbols (A to F). In hexadecimal no numerical symbols that represent If values greater than nine. This uses the English alphabet like Hexadecimal A = Decimal 10, and Hexadecimal F = Decimal 15. We can also convert from Decimal to Hexadecimal, Binary to Decimal and Decimal to Binary, Decimal to Octal and Octal to Decimal, Octal to Binary and Binary to Octal also can be done.
In this post, we will write the programs for hexadecimal to decimal conversion in Python language. We will be given a hexadecimal number and the python program to convert the given hexadecimal number into an equivalent decimal number.
Hexadecimal | Decimal |
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
Hexadecimal | Decimal |
A | 10 |
B | 11 |
C | 12 |
D | 13 |
E | 14 |
F | 15 |
10 | 16 |
11 | 17 |
5E | 94 |
3E8 | 1000 |
Mathematically,
Hexadecimal number = 3E8
Decimal number = 3 x 162 + 14 x 161 + 8 x 160
= 768 + 224 + 8 = 1000
Python Program to convert Decimal to Hexadecimal using While loop
This python program using a while loop to convert hexadecimal to decimal. We can also take the help of a user-defined function. A function is a block of code that performs a specific task. We will take a hexadecimal number while declaring the variables. Python program to convert hexadecimal to a decimal using while loop and finally, the result will be displayed on the screen.
# Python program to convert hexadecimal to decimal
def HexDecimal(num): #user-defined function
ch = dec = i = 0
length = len(num) - 1
while length >= 0:
if num[length]>='0' and num[length]<='9':
temp = int(num[length])
elif num[length]>='A' and num[length]<='F':
temp = ord(num[length]) - 55
elif num[length]>='a' and num[length]<='f':
temp = ord(num[length]) - 87
else:
ch = 1
break
dec = dec + (temp * (16 ** i))
length = length - 1
i = i+1
return dec
# take inputs
num = input('Enter hexadecimal number: ')
# calling function and display result
print('The decimal value is =', HexDecimal(num))
Output for the different input values:-
Enter hexadecimal number: 19
The decimal value is = 25
Enter hexadecimal number: 5D
The decimal value is = 93
Enter hexadecimal number: 12b
The decimal value is = 299
Enter hexadecimal number: 2f65a3
The decimal value is = 3106211
Convert Hexadecimal to Decimal using int() function
This is the simplest and easiest program in python because this program is using a pre-defined function. We will take the hexadecimal number when declaring the variable and print the decimal value of the number using the int() function.
# Python program to convert hexadecimal to decimal
def HexDecimal(n): #user-defined function
return int(n, 16)
# take inputs
num = input('Enter hexadecimal number: ')
# calling function and display result
print('The decimal value is =', HexDecimal(num))
Output:-
Enter hexadecimal number: 42f
The decimal value is = 1071
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!