# 4 Best Ways to Convert Hexadecimal to Decimal in Python

Contents

## Introduction

In python, we have been discussing many concepts. Sometimes, we occur in a situation where we need to find the decimal value of the hexadecimal number. So In this tutorial, we will be discussing the conversion of hexadecimal to decimal in python. As the conversion of elements has been a handy utility as it offers it in a much simpler way than other languages.

## What is hexadecimal to decimal conversion in Python?

Hexadecimal to Decimal conversion is the conversion in which we will convert the hexadecimal string into a decimal value with the help of different functions and different ways.

The hexadecimal number system is the numeral system made up of 16 characters or symbols. It is also known by the name hex in the python programming language.

## 4 Different ways to convert hexadecimal to decimal in Python

Here, we will be discussing all the ways through which we can convert hexadecimal to decimal value:

### 1. Using int() for Converting hexadecimal to decimal in Python

Python module provides an int() function which can be used to convert a hex value into decimal format. It accepts 2 arguments, i.e., hex equivalent and base, i.e. (16).

int() function is used to convert the specified hexadecimal number prefixed with `0x` to an integer of base 10.

If the hexadecimal number is in the form of string format, then the second parameter must identify the base, i.e., 16 of the specified number in string format. Let us look at the example for understanding the concept in detail.

```# hexadecimal string
hex = '0F'

# conversion
dec = int(hex, 16)

print('Value in decimal:', dec)
```

Output:

``````Value in hexadecimal: 0F
Value in decimal: 15``````

Explanation:

• Firstly, we will take the hexadecimal input in the variable hex.
• Then, we will apply the int() function.
• Inside which we have passed both the parameters.
• At last, we have printed the hexadecimal value and the converted decimal value.
• Hence, you can see both the values in the output.

### 2. Using ast.literal_eval() for Converting hexadecimal to decimal in Python

In this example, we will be using the literal evaluation function, which helps us to predict the base and converts the number string to its decimal number format. The literal_eval() function is available in ast module. Let us look at the example for understanding the concept in detail.

```#import literal_eval from ast module
from ast import literal_eval

string = '0xF'

# conversion
dec = literal_eval(string)

print("The hexadecimal string is: ", string)
print("The decimal number is: ", dec)
```

Output:

``````The hexadecimal string is :  0xF
The decimal number is :  15``````

Explanation:

• Firstly, we will import the literval_eval() function from the ast library,
• Then, we will take the hexadecimal string in the string variable.
• After that, we will apply the literal_eval() function.
• Literal_eval() function will predict the base and converts the number string to its decimal number format.
• At last, we will print the hexadecimal value and the converted decimal value.
• Hence, you can see both the values in the output.

### 3. Using a dictionary for Converting hexadecimal to decimal in Python

In this example, we will be using a dictionary to convert hexadecimal to decimal. We will be making a dictionary in which we will write all the predefined values of the hexadecimal table in it. After that, we will apply for loop and convert the values into the required format. Let us look at the example for understanding the concept in detail.

```<pre class="wp-block-syntaxhighlighter-code">hex_to_dec_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}

hex = input("Enter the hexadecimal number: ").strip().upper()
dec = 0

#computing max <a href="https://www.pythonpool.com/numpy-power/" target="_blank" rel="noreferrer noopener">power</a> value
length = len(hex) -1

for digit in hex:
dec += hex_to_dec_table[digit]*16**length
length -= 1

print("Decimal value is : ",dec)</pre>
```

Output:

``````Enter the hexadecimal number: af
Decimal value is :  175``````

Explanation:

• Firstly, we will define the dictionary with the decimal related to hexadecimal values in the variable hex_to_dec_table.
• Then, we will take the input from the user and convert it into the uppercase format.
• We will take a variable dec which is set to 0.
• Then, we will calculate the max power value inside the variable named length.
• After that, we will apply for loop with the given conditions.
• At last, we will print the dec value, which is also called the decimal value.
• Hence, you can see the converted value as the output.

### 4. Using while loop for Converting hexadecimal to decimal in Python

In this example, we will be using a while loop to convert hexadecimal to a decimal value. Firstly, we will take the hexadecimal input. Then, we will take three variables, c, count, and ‘i’, all equal to 0. After that, we will apply while loop with all the conditions inside it. At last, we will check the value of c. If it is equal to 0, we will print the value of the count—otherwise, invalid input. Let us look at the example for understanding the concept in detail.

```hex = input("Enter Hexadecimal Number: ")

c = count = i = 0
len = len(hex) - 1
while len>=0:
if hex[len]>='0' and hex[len]<='9':
rem = int(hex[len])
elif hex[len]>='A' and hex[len]<='F':
rem = ord(hex[len]) - 55
elif hex[len]>='a' and hex[len]<='f':
rem = ord(hex[len]) - 87
else:
c = 1
break
count = count + (rem * (16 ** i))
len = len - 1
i = i+1

if c == 0:
print("\nDecimal Value = ", count)
else:
print("\nInvalid Input!")
```

Output:

``````Enter Hexadecimal Number: ff

Decimal Value =  255``````

Explanation:

• Firstly, we will take the hexadecimal input from the user.
• Then, we will take three variables, c, count, and I, which are set to 0.
• Then, we will calculate the max power value inside the variable named length.
• After that, we will apply the while loop with the given condition inside it.
• Then, we will check if the value of c is equal to 0, we will print the value of count. Otherwise, invalid input.
• Hence, you can see the decimal value as the output.

Also, Read >> Integer to Binary Conversion in Python

## Conclusion

In this tutorial, we have learned about the concept of converting a hexadecimal value to a decimal value. We have seen all the ways through which we can convert hexadecimal to a decimal value. All the ways are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Subscribe
Notify of 