Categories
Python Answers

How to convert hex string to int in Python?

Spread the love

Sometimes, we want to convert hex string to int in Python.

In this article, we’ll look at how to convert hex string to int in Python.

How to convert hex string to int in Python?

To convert hex string to int in Python, we can call the int function.

For instance, we write:

x = int("deadbeef", 16)
y = int("0xdeadbeef", 0)
print(x)
print(y)

We call int with the hex number with or without the 0x prefix.

If we skip the 0x prefix, then we need to pass in the base as the 2nd argument.

Otherwise, we can pass in 0 for the 2nd argument.

Therefore, x and y are both 3735928559.

Conclusion

To convert hex string to int in Python, we can call the int function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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