How to Convert Int to Bytes in Python? [5 Methods]

Are you interested in learning how to convert int to bytes in Python? In this Python article, I will explain how to convert int to bytes with illustrative examples.

Python bytes are a sequence of integers in the range of 0-255. They are an immutable sequence data type, meaning they cannot be changed once a bytes object is created.

Read this article to learn five different methods to convert int to bytes in Python.

  • Using the to_bytes Method
  • Using the struct Module
  • Using Bitwise Operators
  • Using a ByteArray and Bitwise Shift
  • Using List Comprehension

Python program to convert int to bytes using the to_bytes() method

In Python, the to_bytes() method takes the integer value as the first input argument, the length of the output bytes object as the second input argument, and the byte order as the third input argument.

Below is Python’s full code to convert int to bytes using the to_bytes() method.

int_number = 4578
converted_to_bytes = int_number.to_bytes(2, 'big')
print(converted_to_bytes)

I used the .to_bytes() method to convert the integer that specifies the number of bytes you want the resulting byte object. Where ‘big’ indicates the byte order.

converted_to_bytes = int_number.to_bytes(2, 'big')

Output:

b'\x11\xe2'

Refer to the screenshot below to see the output of the Python program.

How to Convert Int to Bytes to Python

How to convert int to bytes using the struct module in Python

This is another approach to convert int to bytes using the struct module in Python. The struct module in Python is used for packing and unpacking data from C-like structures. The pack() function in Python is particularly useful for converting an integer into bytes.

READ:  How to get an IP from a URL in Python? [+5 Examples]

It provides great flexibility in handling binary data. It’s a built-in module available in Python3. This is the complete Python program to convert int to bytes using the struct module.

 import struct

int_num = 4192
int_converted_byte = struct.pack('>H', int_num)
print(int_converted_byte)

The pack() function in Python calls packs of data into a bytes object according to the format where H indicates bytes order, meaning the most significant byte (i.e., highest-order byte) that comes first.

int_converted_byte = struct.pack('>H', int_num)

Output:

b'\x10`'

You can see the output of the Python program in the below screenshot.

Python program to convert int to bytes using struct module

Convert int to bytes in Python using the bitwise operator

In Python, bitwise operators are used to perform bitwise calculations. It converts the integer to binary, and then operations are performed bit-by-bit.

Here is the full code of Python to convert int to bytes using the bitwise operator in Python.

integer_value = 2096
byte_value = bytes([(integer_value >> 8) & 0xFF, integer_value & 0xFF])
print(byte_value)

Here, list comprehension is used with a list of two elements. Then, it will extract the most significant byte of integer_value and shift the bits of integer_value to the right by 8 positions. Now apply a bitwise AND operation with 0xFF, which masks all but the least significant 8 bits.

byte_value = bytes([(integer_value >> 8) & 0xFF, integer_value & 0xFF])

Output:

b'\x080'

You can see the output provided in the below screenshot.

Convert int to bytes in Python using the bitwise operator

Python program to convert int to bytes using byte array and bitwise shift

Another method is to convert int to bytes using byte array and bitwise shift in Python. This method enhances flexibility in creating byte sequences but might be excessive for simple conversions.

READ:  How to Get the Last N Elements of a List in Python [7 Ways]

In Python, these operators shift the bits of a number left or right, multiplying or dividing the number by two.

To do this, I will use this Python source code.

int_number = 4684

byte_array = bytearray()
byte_array.append((int_number >> 8) & 0xFF)
byte_array.append(int_number & 0xFF)

bytes_object = bytes(byte_array)
print(bytes_object)

Output:

b'\x12L'

The screenshot below shows the Python program’s output to convert int to byte using the byte array and bitwise shift.

How to convert int to bytes using byte array and bitwise shift in Python

How to convert int to bytes in Python using list comprehension

In Python, list comprehension provides shorter syntax to create a list based on the value of the existing list.

Here is the complete source code of Python to convert int to bytes using list comprehension.

integer_value = 8128
int_to_byte_value = bytes([integer_value >> i & 0xff for i in (8,0)])
print(int_to_byte_value)

Output:

b'\x1f\xc0'

You see the output in the screenshot below to convert int to bytes in Python.

Python program to convert int to bytes using list comprehension

Conclusion

I hope you find this article helpful as it covers how to convert int to bytes in Python using five different methods, such as the to_bytes()method, struct module, bitwise operator, byte array with bitwise shift, and list comprehension with their examples.

Each method has its advantages. Now, the choice of the method depends on the requirement of the code.

You may also like to read: