Sometimes, we want to convert int to bytes in Python.
In this article, we’ll look at how to convert int to bytes in Python.
How to convert int to bytes in Python?
To convert int to bytes in Python, we can use the to_bytes method.
For instance, we write
b = (1024).to_bytes(2, byteorder='big')
to convert int 1024 to a byte string with to_bytes called with 2.
We specify that the byte string is big endian by setting byteorder to 'big'.
Conclusion
To convert int to bytes in Python, we can use the to_bytes method.