How To Convert Hexadecimal to Decimal In Python

Hexadecimal number is a special type of number system which represents data in alphanumeric code. Hexadecimal number represented by 0-9 numbers and A-Z or a-z  alphabet. Hexadecimal number has base 16. Whereas decimal number is a human interpretable number that is represented by 0-9 digits whose base is 10.     

Example: Following numbers are represented in the decimal numbers.

46, 58,24,82,94

Example: Following numbers are represented in the hexadecimal numbers.

A2, 1CD, BC24, EFF, B2D

Following table represents the hexadecimal number and its equivalent decimal.

Hexadecimal NumberEquivalent Decimal Number
(F)16(15)10
(2A)16(42)10
(4B)16(75)10

Why Is There A Need To Convert Hexadecimal to Decimal In Python

In a computer system, hexadecimal numbers are used to represent memory address, hardware port address and network MAC address. We are required to convert the hexadecimal code to the decimal number for the following reasons. 

  • Security Code Interpretation: In computer security, digital signature is computed in hexadecimal code. Therefore to process that code we convert hex code to decimal numbers.  
  • MAC Address Interpretation: For the network system, MAC or physical address is represented in hexadecimal code. But this code is not understood by humans. So, it is required to convert into decimal form.    
  • Color Code Interpretation: In computer graphics or web pages, color components are represented by hexadecimal numbers. To process different color codes in number than conversion is also required. 

Approaches to Convert Hexadecimal to Decimal

Numerous approaches may be used to convert hexadecimal code to decimal numbers. There are some approaches listed and discussed below. 

  • Approach-1: Using Int() Built-in Function
  • Approach-2: Using For Loop
  • Approach-3: Using “ast” library and literal.eval()
  • Approach-4: Using Iteration

Lets dive in details for each approach.

Approach-1: Using Int() Built-in Function

Python offers an inbuilt function named Int() to convert hexadecimal  numbers to decimal numbers. The Int() function takes a hex code with base as two arguments and returns an equivalent decimal number. Following algorithm describes the steps involved for the conversion of hexadecimal to decimal numbers.

Algorithm:

Step-1: Taking hexadecimal code as an input string from the user and store into a variable.

Step-2: Call Int() function and pass hex string and base 16 as arguments.

Step-3: Decimal number is returned by the Int() function.

To demonstrates this approach, a program is created which code is given below:

Code:

# Python program to demonstrate conversion of Hexadecimal to Decimal using Int() function 
hexcode=(input('Enter a Hexadecimal number: '))       # It ask hexadecimal code as an input from the users
decimal=int(hexcode,16)                             # Int() function convert given hexadecimal to decimal number
print('Original Hexadecimal number is: ', hexcode)  # It display original hexadecimal number
print('Decimal number equivalent to given Hexadecimal is: ', decimal)  # It display resulted decimal number

Output:

Enter a Hexadecimal number: AA
Hexadecimal number is:  AA
Decimal number equivalent to given Hexadecimal is:  170

Code Explanation:

  • The hexadecimal number is asked from the user and stores its value in the hexcode variable.
  • Int() function is called with the arguments hexcode and base value is 16.
  • The result of the function  is stored in the variable  named ‘decimal’ that is a decimal number.
  • The original hexadecimal and decimal number is displayed using the print function. 

Approach-2: Using For Loop

With the help of a for loop, we can convert hexadecimal numbers to decimal numbers. An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take a hexadecimal number from the user.

Step-2: Create a dictionary which contains numbers for each hex character.

Step-3: Take each character from the input hexadecimal code and match with  the dictionary element to compute the number in a repeated manner using a for loop.   

Step-4 Decimal number is computed and displayed.

Code:

# Python program to demonstrate conversion of Hexadecimal to Decimal using For Loop  
hexdata = {'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,'a':10, 'b': 11,
         'b': 12, 'd': 13, 'e': 14, 'f': 15}
hexcode=(input('Enter a Hexadecimal number: ')).strip()       # It ask hexadecimal code as an input from the users
decimal=0
s=len(hexcode)-1
for n in hexcode:
    decimal = decimal + hexdata[n]*16**s
    s = s - 1
print('Original Hexadecimal number is: ', hexcode)  # It display original hexadecimal number
print('Decimal number equivalent to given Hexadecimal is: ', decimal)  # It display resulted decimal number

Output:

Enter a Hexadecimal number: aA
Original Hexadecimal number is:  aA
Decimal number equivalent to given Hexadecimal is:  170

Code Explanation:

  • Ask hexadecimal number from the user
  • A dictionary named hexdata is created which contains numbers for each hex character..
  • Take each character from the input hexadecimal code and match with  the dictionary element to compute the number in a repeated manner using a for loop.   
  • The original hexadecimal and decimal number is displayed using the print function.

Approach-3: Using  ast library and literal.eval() Function

Python also offers an “ast” library which allows conversion of hexadecimal number to decimal number using literal.eval() function. Literal.eval() takes hexadecimal code as an argument and returns a decimal number as a result. This approach is defined in the following algorithm.

Algorithm: 

Step-1: Takes a hexadecimal code from the user.

Step-2: Call literal.eval() function and pass hex string as an argument.

Step-3: Decimal number is returned by the literal.eval() function.

Code:

# Python program to demonstrate conversion of Hexadecimal to Decimal using ast library literal.eval() function  
from ast import literal_eval
hexcode='0xBB'       # It declare hexadecimal code as an input 
decimal=literal_eval(hexcode)
print('Original Hexadecimal number is: ', hexcode)  # It display original hexadecimal number
print('Decimal number equivalent to given Hexadecimal is: ', str(decimal))  # It display resulted decimal number

Output:

Original Hexadecimal number is:  0xBB
Decimal number equivalent to given Hexadecimal is:  187

Code Explanation:

  • A hexadecimal number is a declared number from the user.
  • Literal.eval() function is called with the arguments hexcode and base value is 16.
  • The result of the function  is stored in the variable  named ‘decimal’ that is a decimal number.
  • The original hexadecimal and decimal number is displayed using the print function. 

Approach-4: Using Iteration

With the help of repetition statements for example while loop we can also convert a given hexadecimal number into a decimal number. In this approach, a hexadecimal number is asked from the user and a decimal number is obtained. An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Taking the hexadecimal number as an input from the user and storing the value into a variable.

Step-2: Take each character from the input hexadecimal code and match with a predefined element to compute the number in a repeated manner using a while loop.   

Step-3  Decimal number is computed and displayed.

Code:

# Python program to demonstrate conversion of Hexadecimal to Decimal using Iteration 
hexcode=(input('Enter a Hexadecimal number: '))       # It ask hexadecimal code as an input from the users
counter = 0
counter1=k=0
size = len(hexcode) - 1
while size >= 0:
    if hexcode[size] >= '0' and hexcode[size] <= '9':
        result = int(hexcode[size])
 
    elif hexcode[size] >= 'A' and hexcode[size] <= 'F':
        result = ord(hexcode[size]) - 55
 
    elif hexcode[size] >= 'a' and hexcode[size] <= 'f':
        result = ord(hexcode[size]) - 87
    else:
        counter = 1
        break
    counter1 = counter1 + (result * (16 ** k))
    size = size - 1
    k = k+1
print('Original Hexadecimal number is: ', hexcode)  # It display original hexadecimal number
print('Decimal number equivalent to given Hexadecimal is: ', result)  # It display resulted decimal number

Output:

Enter a Hexadecimal number: A
Original Hexadecimal number is:  A
Decimal number equivalent to given Hexadecimal is:  10

Code Explanation:

  • The decimal number is asked from the user and stores its value in the ‘hexcode’ variable.
  • Take each character from the input hexadecimal code and match with  the element to compute the number in a repeated manner using a while loop.   
  • The original hexadecimal and decimal number is displayed using the print function.

Best Approach- “ast” library literal.eval( )

The best approach is to convert a hexadecimal number to decimal number using an in-built function named literal.eval(). It is the best approach because:

  • It used to convert any input number to a hexadecimal number such as binary, octal and decimal.
  • It is  a short and very convenient way to convert hexadecimal to decimal numbers.
  • It offers a short length of code.

Sample problems related to convert hexadecimal to decimal

Sample Problem-1: Using Approach-1

Problem Definition: Consider a network system that MAC address is represented in the hexadecimal code. Therefore, create an algorithm and python code to demonstrate conversion of hexadecimal to decimal number. 

Solution: An algorithm for this problem is represented in the following steps.

 Step-1: Taking decimal number as an input from the user and store the value of given decimal number into a variable.

Step-2: Call Int() function and pass a decimal number as an argument.

Step-3: Hexadecimal number is returned by the Int() function.

Code:

# Python program to demonstrate conversion of Hexadecimal to Decimal using approach-1 
MACAddress=(input('Enter a Hexadecimal number: '))       # It ask hexadecimal code as an input from the users
simpleaddress=int(MACAddress,16)                             # Int() function convert given hexadecimal to decimal number
print('MAC Address in Hexadecimal number is: ', MACAddress)  # It display original hexadecimal number
print('Decimal number equivalent to given MAC Address is: ', simpleaddress)  # It display resulted decimal number

Output:

Enter a Hexadecimal number: Ab3
MAC Address in Hexadecimal number is:  Ab3
Decimal number equivalent to given MAC Address is:  2739

Code Explanation:

  • The hexadecimal number is asked from the user and stores its value in the hexcode variable.
  • Int() function is called with the arguments hexcode and base value is 16.
  • The result of the function  is stored in the variable  named ‘decimal’ that is a decimal number.
  • The original hexadecimal and decimal number is displayed using the print function. 

Sample Problem-2: Using Approach-2

Problem Definition:  Web designers wish to make web pages in different colors. Color code is represented in hexadecimal code which requires conversion in decimal form. Therefore create an algorithm and python code to demonstrate conversion of decimal numbers to octal representations.

Solution: An algorithm for this approach is represented in the following steps.

Algorithm: 

Step-1: Take a hexadecimal number from the user.

Step-2: Create a dictionary which contains numbers for each hex character.

Step-3: Take each character from the input hexadecimal code and match with  the dictionary element to compute the number in a repeated manner using a for loop.   

Step-4 Decimal number is computed and displayed.

Code:

# Python program to demonstrate conversion of Hexadecimal to Decimal using approach-2  
hexdata = {'A': 10, 'B': 11,
         'C': 12, 'D': 13, 'E': 14, 'F': 15,'a':10, 'b': 11,
         'b': 12, 'd': 13, 'e': 14, 'f': 15,'0': 0, '1': 1, '2': 2, '3': 3,
         '4': 4, '5': 5, '6': 6, '7': 7,
         '8': 8, '9': 9}
colorcode=(input('Enter a Hexadecimal number: ')).strip()       # It ask hexadecimal code as an input from the users
decimal=0
s=len(colorcode)-1
for n in colorcode:
    decimal = decimal + hexdata[n]*16**s
    s = s - 1
print('Color Code in Hexadecimal number is: ', colorcode)  # It display original hexadecimal number
print('Decimal number equivalent to given Hexadecimal is: ', decimal)  # It display resulted decimal number

Output:

Enter a Hexadecimal number: aD
Color Code in Hexadecimal number is:  aD
Decimal number equivalent to given Hexadecimal is:  173

Code Explanation:

  • Ask the color code in hexadecimal numbers from the user.
  • A dictionary named hexdata is created which contains numbers for each hex character..
  • Take each character from the input hexadecimal code and match with  the dictionary element to compute the number in a repeated manner using a for loop.  
  • The original hexadecimal and decimal number is displayed using the print function.

Sample Problem-3:Using Approach-3

Problem Definition: Consider a computer system where the memory address is represented in hex code, To process hex code in number, it is required to convert to decimal number. Therefore create an algorithm and  python code to demonstrate how to convert a hexadecimal to decimal number.

Solution: An algorithm for this problem is represented in the following steps.

Algorithm: 

Step-1:  A hexadecimal code is declared.

Step-2: Call literal.eval() function and pass hex string as an argument.

Step-3: Decimal number is returned by the literal.eval() function.

Code:

# Python program to demonstrate conversion of Hexadecimal to Decimal using ast library literal.eval() function  
from ast import literal_eval
hexcode='0xA'       # It declare hexadecimal code as an input 
decimal=literal_eval(hexcode)
print('Original Hexadecimal number is: ', hexcode)  # It display original hexadecimal number
print('Decimal number equivalent to given Hexadecimal is: ', str(decimal))  # It display resulted decimal number

Output:

Original Hexadecimal number is:  0xA
Decimal number equivalent to given Hexadecimal is:  10

Code Explanation:

  • A hexadecimal number is a declared number from the user.
  • Literal.eval() function is called with the arguments hexcode.
  • The result of the function  is stored in the variable  named ‘decimal’ that is a decimal number.
  • The original hexadecimal and decimal number is displayed using the print function.

Sample Problem-4: Using Approach-4

Problem Definition: A company wants to implement a digital calculator to convert a number system to another number system such as binary, octal, hexadecimal etc. Therefore, create an algorithm and  python code to demonstrate hexadecimal to decimal conversions.

Solution: An algorithm for this problem is represented in the following steps.

Algorithm: 

Step-1: Taking the hexadecimal number as an input from the user and storing the value into a variable.

Step-2: Take each character from the input hexadecimal code and match with a predefined element to compute the number in a repeated manner using a while loop.   

Step-3  Decimal number is computed and displayed.

Code:

# Python program to demonstrate conversion of Hexadecimal to Decimal using approach-4
hexcode=(input('Enter a Hexadecimal number: '))       # It ask hexadecimal code as an input from the users
counter = 0
counter1=k=0
size = len(hexcode) - 1
while size >= 0:
    if hexcode[size] >= '0' and hexcode[size] <= '9':
        result = int(hexcode[size])
 
    elif hexcode[size] >= 'A' and hexcode[size] <= 'F':
        result = ord(hexcode[size]) - 55
 
    elif hexcode[size] >= 'a' and hexcode[size] <= 'f':
        result = ord(hexcode[size]) - 87
    else:
        counter = 1
        break
    counter1 = counter1 + (result * (16 ** k))
    size = size - 1
    k = k+1
print('Original Hexadecimal number is: ', hexcode)  # It display original hexadecimal number
print('Decimal number equivalent to given Hexadecimal is: ', result)  # It display resulted decimal number

Output:

Enter a Hexadecimal number: A
Original Hexadecimal number is:  A
Decimal number equivalent to given Hexadecimal is:  10

Code Explanation:

  • The decimal number is asked from the user and stores its value in the ‘hexcode’ variable.
  • Take each character from the input hexadecimal code and match with  the element to compute the number in a repeated manner using a while loop.   
  • The original hexadecimal and decimal number is displayed using the print function.

Conclusion

Python supports many number systems representations and their conversions using inbuilt functions and other methods. A hexadecimal number may be converted into a decimal number.

In this article, many approaches to convert a hexadecimal number to decimal number were presented with an algorithm, python code, output and its explanation. An in-built function named literal.eval() function of ast library approach is considered as the best approach because it offers a shortened and convenient way.