# 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)) `

Output :

```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)) `

Output :

```The original string : A
The decimal number of hexadecimal string : 10
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.