Hexadecimal to decimal Python
In this post, you will learn how to convert hexadecimal to decimal using the Python programming language.
Hexadecimal describes a base-16 number system. The hexadecimal number system is generally used in computer programming and microprocessors. It is also helpful to describe the colours on web pages. The main disadvantage of the hexadecimal number system is that it may not be easy to read and write for people, and it is also difficult to perform operations like multiplications, divisions using the hexadecimal number system.
A decimal number is a base-10 number system where the range of numbers is 0,1,2,3,4,5,6,7,8,9. The main advantages of the decimal number system are that it is easy-to-read, used by humans, and easy to manipulate.
Convert hexadecimal to decimal using int() function
In the given Python program, we have used the int() function to convert the hexadecimal to decimal. The int() function can help in converting various values to decimal integers.
The int() function converts the predetermined hexadecimal number prefixed with 0x to a whole number of base 10.
print("0xA : ",int(0xA))
print("0x1d : ",int(0x1d))
print("0xff : ",int(0xff))
print("0xc : ",int(0xc))
print("0x3d : ",int(0x3d))
Output of the above code:
0xA : 10
0x1d : 29
0xff : 255
0xc : 12
0x3d : 61
Convert hexadecimal to decimal using ast.literal_eval() function
The ast.literal_eval() function can evaluate string literals in Python. The literal_eval() function is available in the ast module. In the given example, we have called the ast.literal_eval(source) function to predict the base and convert the number string to its decimal number format. The function returns the integer values when a hexadecimal string is passed to it.
import ast
a = ast.literal_eval('0xffa')
print("0xffa : ",int(a))
b = ast.literal_eval('0x1d')
print("0x1d : ",int(b))
c = ast.literal_eval('0x3d')
print("0x3d : ",int(c))
e = ast.literal_eval('0x39')
print("0x39 : ",int(e))
Output of the above code:
0xffa : 4090
0x1d : 29
0x3d : 61
0x39 : 57
Convert hexadecimal to decimal using while loop
In the given Python program, we use a while loop to convert hexadecimal to decimal.
# Convert Hexadecimal to decimal using
# user-defined function and while loop
def HexDecimal(num):
count = dec = x = 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:
count = 1
break
dec = dec + (temp * (16 ** x))
length = length - 1
x = x+1
return dec
# user inputs
num = input('Enter hexadecimal number: ')
# calling function and display result
print('The decimal value : ', HexDecimal(num))
Output of the above code:
Enter hexadecimal number: 0xff
The decimal value: 255
Enter hexadecimal number: 0x3d
The decimal value: 61
Enter hexadecimal number: 5D
The decimal value: 93
Related Articles
Python program to reverse a number
Find Power of a Number in Python
Perfect number in Python
Python program to add two numbers
Python program for Prime Number
Prime factors of a number in Python
Python program to multiply two numbers
Python convert list to numpy array
Python Numpy Array Shape
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Trigonometric functions Python Numpy
Python Pandas Dataframe to CSV
Python Pandas DataFrame
Convert list to dictionary Python
Dictionary inside list Python
Convert dictionary to dataframe Python
Python Pandas CSV to Dataframe
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module