Python program to convert hex string to decimal
In Python, element conversion has been a very useful utility as it offers it in a much simpler way that 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 : Using int()
This function can be used to perform this particular task, adding an argument (16) this function can convert hexadecimal string number to base sixteen and convert it into integer at the same time.
# Python3 code to demonstrate # converting 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)) |
The original string : A The decimal number of hexadecimal string : 10
Method #2 : Using ast.literal_eval()
We can perform this particular function by using literal evaluation function that predicts the base and converts the number string to its decimal number format.
# 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)) |
The original string : A The decimal number of hexadecimal string : 10
Recommended Posts:
- Python program to convert float decimal to Octal number
- Python | Program to convert a tuple to a string
- Python | Program to convert String to a List
- Python program to convert a list to string
- Quickly convert Decimal to other bases in Python
- Python program to convert camel case string to snake case
- Python program to covert decimal to binary number
- Python | Convert String to Binary
- Python | Convert String to bytes
- Python | Convert None to empty string
- Python program to convert POS to SOP
- Python | Convert a list of characters into a string
- Python | Convert Bytearray to Hexadecimal String
- Python | Convert dictionary object into string
- Python | Convert tuple records to single string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




