Skip to content

Python Program To Convert Hex String To Decimal

  • by
Python-hex-decimal

Python is a highly versatile and robust programming language that allows its users to convert one element into another.

Other than that, a good and efficient programmer is well aware of the knowledge of interconversions Python Program To Convert Hex String To Decimal.

Contents

Python Program To Convert Hex String To Decimal?

The decimal format does not have any spaces between groups and numbers so it’s easier for humans to read than hexadecimal. Decimals also allow negative values which are not possible with HEX strings!

The code below will show how we can convert our original text from a string into an array of numbers:

Hat/Input = “Input” + raw_input( ) ; String midday contains 12 null characters ( ‘\000’ ).

We need this because there isn’t enough room in the buffer after converting each individual digit individually without losing anything important like accuracy or precision when doing division.

Before we discuss the conversion methods, let us discuss what a hex string even is.

According to electronics tutorials the “Hexadecimal” or simply “Hex” numbering system uses the Base of 16 system and are a popular choice for representing long binary values because their format is quite compact and much easier to understand compared to the long binary strings of 1’s and 0’s.

Converting a hexadecimal number to a decimal number is simple. But how do you achieve that using a programming language?

This article will discuss how you can convert a hexadecimal string to a decimal number using python language. So without further ado, let us get straight into it.

Using INT

First, and probably the easiest method, is by using int().

To convert a hex string into a decimal number, all you have to do is add an argument, which is 16.

This means that the function can convert the hex string to base 16 and convert it into an integer at the same time. For better understanding, a source code is given below.

# Python3 code to demonstrate
# converting a hexadecimal string to decimal
# Using int()
  
# 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

Using Ast. literal.eval

Another excellent method that you can use to convert a hex string into a decimal is by making use of the ast.literal_eval() function.

This particular function performs this task by predicting the base and converting the number string to its decimal number format.

I am giving  An example code below for your better understanding.

# Python3 code to demonstrate
# converting hexadecimal string to decimal
# Using ast.literal_eval()
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

Read More: Python merge dictionaries with the same value

A FINAL WORD:

There are numerous ways through which you can convert a hexadecimal string into a decimal number, but the methods mentioned above are perhaps the best ones in this regard

Read More: Find the average of a list in python

Leave a Reply

Your email address will not be published. Required fields are marked *