W3docs

Convert hex string to integer in Python

You can use the int() function in Python to convert a hex string to an integer.

You can use the int() function in Python to convert a hex string to an integer. The int() function takes two arguments: the first is the hex string, and the second is the base of the number system used in the string (base 16 for hex).

Here's an example:

Convert a hex string to an integer in Python by the int function

python— editable, runs on the server

The output will be:


25759

You can also use the int.from_bytes() function to convert hex string to integer by specifying byte order as 'big' or 'little'.

Convert a hex string to an integer in Python by the from_bytes method

python— editable, runs on the server

The output will be:


25759

Note that the int() function will raise a ValueError if the hex string contains any non-hexadecimal characters.