How to convert a string of bytes into an int with Python?

Sometimes, we want to convert a string of bytes into an int with Python.

In this article, we’ll look at how to convert a string of bytes into an int with Python.

How to convert a string of bytes into an int with Python?

To convert a string of bytes into an int with Python, we can use the int.from_bytes method.

For instance, we write

n = int.from_bytes(b'yxccxa6xbb', byteorder='big')

to call int.from_types with a byte string and the byteorder argument set to the string value for the endianness of the byte string.

We can set byteorder to 'little' if we have a little endian byte string.

Conclusion

To convert a string of bytes into an int with Python, we can use the int.from_bytes method.