Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to convert hex string to decimal

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 22 Aug, 2022
Improve Article
Save Article

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 and 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. 

Method 1: Converting hexadecimal to decimal in Python 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

Method 2: Converting hexadecimal to decimal in Python 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

Method 3: Converting hexadecimal to decimal in Python using without In-build function

We can perform this particular task also by 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

Method 4: Converting hexadecimal 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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!