In Python, element conversion has been a very useful utility as it offers it in a much simpler way than in other languages. This makes Python a robust language; hence, knowledge of interconversions is always a plus for a programmer. This article discusses the hexadecimal string to a decimal number. Let’s discuss certain ways in which this can be performed.
Example
Input: 3A7B
Output: 15099
Explanation: In this, we have converted a hex string '3A7B' to decimal.
Python Hex Str to Decimal
- Using Int()
- Using Ast.literal_eval()
- Using Without an In-built function
- Using Loop
- Using Reduce() function
- Using Math Module
Python Convert Hex Str to Decimal using int()
This Python int() function can be used to perform this particular task, adding an argument (16) this function can convert a hexadecimal string number to base sixteen and convert it into an integer at the same time.
Python3
# initializing string test_string = 'A' # printing original string print ( "The original string : " + str (test_string)) # using int() # converting hexadecimal string to decimal res = int (test_string, 16 ) # print result print ("The decimal number of hexadecimal \ string : " + str (res)) |
Output
The original string : A
The decimal number of hexadecimal string : 10
Time Complexity: O(k), where k is the number of digits
Space Complexity: O(1)
Python Convert Hex Str to Decimal using Ast.literal_eval()
We can perform this particular function by using a literal evaluation function that predicts the base and converts the number string to its decimal number format.
Python3
from ast import literal_eval # initializing string test_string = '0xA' # printing original string print ( "The original string : " + str (test_string)) # using ast.literal_eval() # converting hexadecimal string to decimal res = literal_eval(test_string) # print result print ("The decimal number of hexadecimal \ string : " + str (res)) |
Output
The original string : A
The decimal number of hexadecimal string : 10
Time Complexity: O(1), where n is the size of the hex string
Space Complexity: O(1)
Python Convert Hex Str to Decimal using without In-build function
We can perform this particular task also without an In-build function that predicts the base and converts the number string to its decimal number format using a Python dictionary.
Python3
table = { '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 } hexadecimal = input ( "Enter Hexadecimal Number: " ).strip().upper() res = 0 # computing max power value size = len (hexadecimal) - 1 for num in hexadecimal: res = res + table[num] * 16 * * size size = size - 1 print (res) |
Output
Enter Hexadecimal Number: A7
167
Time Complexity: O(n), where n is the size of the hex string
Space Complexity: O(n)
Convert Hex to Decimal in Python using Loop
We can perform this particular function by using a Python loop that predicts the base and converts the number string to its decimal number format.
Python3
# initializing string hex = "A12" c = counter = i = 0 size = len ( hex ) - 1 # loop will run till size while size > = 0 : if hex [size] > = '0' and hex [size] < = '9' : rem = int ( hex [size]) elif hex [size] > = 'A' and hex [size] < = 'F' : rem = ord ( hex [size]) - 55 elif hex [size] > = 'a' and hex [size] < = 'f' : rem = ord ( hex [size]) - 87 else : c = 1 break counter = counter + (rem * ( 16 * * i)) size = size - 1 i = i + 1 print ( "Decimal Value = " , counter) |
Output
Decimal Value = 2578
Time Complexity: O(n), where n is the size of the hex string
Space Complexity: O(1)
Convert Hex to Decimal in Python using Reduce() function
This method uses a list comprehension to convert each character in the hexadecimal string to its integer equivalent using the int() function with base 16. The resulting list of integers is then passed to the reduce() function along with a lambda expression that multiplies each previous element by 16 and adds the current element. The reduce() function iteratively applies this lambda function to the list of integers, resulting in a single integer value that represents the decimal equivalent of the input hexadecimal string
Python3
from functools import reduce hex_string = "2A" decimal = reduce ( lambda x, y: x * 16 + y, [ int (char, 16 ) for char in hex_string]) print (decimal) |
Output
42
Time complexity: The time complexity of this method is O(n), where n is the length of the input hexadecimal string. This is because the list comprehension that creates a list of integers from the input string has a time complexity of O(n), and the reduce() function that performs the iterative multiplication and addition operations also has a time complexity of O(n).
Space auxiliary: The space auxiliary complexity of this method is O(n), where n is the length of the input hexadecimal string. This is because the list comprehension creates a list of integers that has the same length as the input string, and this list is stored in memory until it is passed to the reduce() function. Additionally, the reduce() function creates a single integer value that is also stored in memory. Therefore, the total space used by this method is proportional to the length of the input string.
Convert Hex to Decimal in Python using Math Module
To convert a hexadecimal (hex) string to decimal in Python, you can use the int()
function along with the base
parameter. However, if you want to utilize the math
module for the conversion, you can make use of the math.pow()
function to calculate the decimal value.
Python3
import math hex_str = '3F0A' dec_num = sum ( int (x, 16 ) * math. pow ( 16 , len (hex_str) - i - 1 ) for i, x in enumerate (hex_str)) print (dec_num) |
Output
16138.0
Time Complexity: O(n), where n is the size of the hex string
Space Complexity: O(1)