convert int to bytes in python

Convert int to bytes in Python

In this tutorial, we will look at how to convert an int type object to a bytes type object in Python with the help of some examples.

How to convert int to bytes in Python?

You can use the int class method int.to_bytes() to convert an int object to an array of bytes representing that integer. The following is the syntax –

# int to bytes
int.to_bytes(length, byteorder, signed)

It takes the following arguments –

  • length – The number of bytes to use to represent the integer. If the integer is not representable with the given number of bytes, an OverflowError is raised.
  • byteorder – Determines the byte order used to represent the integer. Use 'big' as the byte order to have the most significant byte at the beginning of the byte array. Use 'little' as the byte order to have the most significant byte at the end of the byte array.
  • signed – Determines wheter to use two’s compliment to represent the integer. It is an optional parameter and is False by default. Helpful in converting signed integers to bytes.

Examples

Let’s look at some examples of using the int.to_bytes() function to convert an integer to bytes.

Using ‘big’ as the byteorder

Let’s convert the integer value 7 to a byte array of length 2 and with “big” as the byteorder.

# integer variable
num = 7
# integer to bytes
num_bytes = num.to_bytes(2, byteorder='big')
# display result and type
print(num_bytes)
print(type(num_bytes))

Output:

b'\x00\x07'
<class 'bytes'>

We get the returned value as bytes with the most significant byte at the beginning.

Using ‘little’ as the byteorder

Let’s use the same example as above but with “little” as the byteorder

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# integer variable
num = 7
# integer to bytes
num_bytes = num.to_bytes(2, byteorder='little')
# display result and type
print(num_bytes)
print(type(num_bytes))

Output:

b'\x07\x00'
<class 'bytes'>

We get the most significant byte at the end of the byte array.

Negative Integers to bytes with signed=True

If you use the default signed=False on a negative integer, you will get an OverflowError.

# integer variable
num = -7
# integer to bytes
num_bytes = num.to_bytes(2, byteorder='big')
# display result and type
print(num_bytes)
print(type(num_bytes))

Output:

---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
Input In [11], in <module>
      2 num = -7
      3 # integer to bytes
----> 4 num_bytes = num.to_bytes(2, byteorder='big')
      5 # display result and type
      6 print(num_bytes)

OverflowError: can't convert negative int to unsigned

To convert negative integers to bytes with the int.to_bytes() function, pass signed=True. It will use two’s complement to represent the integer.

# integer variable
num = -7
# integer to bytes
num_bytes = num.to_bytes(2, byteorder='big', signed=True)
# display result and type
print(num_bytes)
print(type(num_bytes))

Output:

b'\xff\xf9'
<class 'bytes'>

We get the bytes for the negative integer.

For more on the int.to_bytes() function, refer to its documentation.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top