A programmer has to work with different number systems. Different formats are useful for different reasons. Hexadecimal numbers are useful in the front end when working with the CSS properties or when we are working with IP or memory addresses. Decimal numbers are used for usual operations.
A hexadecimal value can be converted to decimal in Python in the following ways:
- Using int()
- Using ast.literal_eval()
- Using a dictionary
- Using reduce()
- Using a math module
In this article, we will learn how we can convert a hexadecimal value to decimal in Python.
Hexadecimal to Decimal Conversion
Hexadecimal number system contains 16 characters also known as hex in Python. The decimal number system contains 10 characters. Consider a hexadecimal number 2B8F that is converted to decimal giving the value 11151. We will convert this hexadecimal value to decimal using some of the Python functionalities.
(2B8F)?? = (2 × 16³) + (11 × 16²) + (8 × 16¹) + (15 × 16?) = (11151)??
How to Convert a Hexadecimal Value to Decimal in Python?
Different built-in Python functions like int() and ast.literal_eval are used for the conversion of a hexadecimal value to a decimal value. There are various ways by which you can convert a hexadecimal value to a decimal one.
Let us go through each method of converting the hexadecimal value to decimal in Python.
Method 1: Using int()
A hexadecimal value can be converted to a decimal value using int(). This will take the hexadecimal value as a string and convert it into its decimal equivalent as an integer. Let us look at the following code to see the working of int():
hexadecimal = "2B8F"
decimal = int(hexadecimal, 16)
print(decimal)
In this code,
- The variable hexadecimal represents a hexadecimal value 2B8F
- int() function converts the hexadecimal value to decimal by giving the second parameter as 16 which represents the base. The value is stored in the variable decimal.
- The value is printed using print(decimal). The output is 11151 which is the decimal equivalent of the hexadecimal value 2B8F.
Output:
Method 2: Using ast.literal_eval()
Ast module can also convert the Hexadecimal value to decimal value in Python. A literal_eval function is provided by the module that takes a string as a parameter, identifies the base of the function, and converts it into its decimal equivalent. Let us look at the following code to see how ast.literal_eval() function does the conversion:
import ast
hexadecimal = "0x2B8F"
decimal = ast.literal_eval(hexadecimal)
print(decimal)
In the above code,
- Firstly, the ast module is imported.
- The hexadecimal value is stored in the variable hexadecimal.
- The variable decimal stores the information provided when ast.literal_eval is applied on hexadecimal.
- The result is displayed to be 11151 by using the function print(decimal).
Output:
Method 3: Using a dictionary
Another way to convert a hexadecimal value to decimal in Python is by using a dictionary. A dictionary of hexadecimal values as their decimal equivalents will be defined. The hexadecimal values are converted to decimal values after applying a loop. Let us at the following code to understand how we perform this task using a dictionary:
dict = {'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}
def converted_value (hexadecimal):
decimal = 0
for i in range(len(hexadecimal)):
decimal += dict[hexadecimal[i]] * (16 ** (len(hexadecimal) - i - 1))
return decimal
print(converted_value('2b8f'))
In this code,
- Define a dictionary dict which defines the hexadecimal values as their decimal equiavalents.
- The variable converted_vaue takes the parameter hexadecimal, and the variable decimal is initialized with value 0
- The for loop iterates for each value of i in the hexadecimal string.
- The hexadecimal value is converted to the decimal value and the result 11151 is displayed using print(converted_value(‘2b8f’))
Output:
Method 4: Using reduce()
reduce() function can convert the hexadecimal value to decimal in Python. A list comprehension does the conversion. The Lambda function multiplies each incoming element with 16 while adding the present value, Through reduce() function, the lambda function is applied on each converted integer giving the decimal output of the hexadecimal number. Let us look at the following code to understand the working of reduce():
from functools import reduce
hexadecimal= "2B8F"
decimal = reduce(lambda x, y: x*16 + y, [int(char, 16) for char in hexadecimal])
print(decimal)
In the code,
- The reduce function is imported from the module functions
- A variable hexadecimal is assigned the value “2B8F”
- Reduce function is applied to a lambda that checks for each value of x and multiplies it with 16 and then return it.
- The result is displayed by printing the value of the decimal variable using print(decimal) that gives the output as 11151.
Output:
Method 5 Using math module
Last method to convert the hexadecimal number to a decimal number is to use the math module. The math module is imported which converts the hexadecimal value to the decimal. Let us look at the following code to see how the math() function works:
import math
hexadecimal= "2B8F"
decimal = sum(int(x, 16) * math.pow(16, len(hexadecimal)-i-1) for i, x in enumerate(hexadecimal))
print(decimal)
In the above code,
- The math module is imported firstly.
- The hexadecimal variable stores the value of 2B8F.
- Each value of x is iterated in the sum function, int function takes 16 as a parameter and converts the hexadecimal value to its decimal equivalent.
- math.pow takes the powers of 16 for each hexadecimal digit, len() calculates the position of digits from right to left while enumerate function takes hexadecimal as a parameter to produce the indices
- The output is 11151 which is displayed using print(decimal).
Output:
In this article, we learned about different methods by which a hexadecimal value can be converted to decimal in Python.
Conclusion
Different Python inbuilt functions like int(), ast.literal_eval() and other methods can be used to convert the hexadecimal number into a decimal one. During the programming journey, it is a usual practice to convert one number system into another. Different number systems are useful in different scenarios. Therefore, we learned how we can convert a hexadecimal number into a decimal one and illustrated each method with relevant examples.