Last updated: Apr 11, 2024
Reading timeยท4 min
Use the int.to_bytes()
method to convert an integer to bytes in Python.
The method returns an array of bytes representing an integer.
num = 2048 my_bytes = num.to_bytes(2, byteorder='big') print(my_bytes) # ๐๏ธ b'\x08\x00'
If your integer is not stored in a variable, make sure to wrap it in parentheses
before calling to_bytes()
.
my_bytes = (2048).to_bytes(2, byteorder='big') print(my_bytes) # ๐๏ธ b'\x08\x00'
The int.to_bytes() method returns an array of bytes representing an integer.
The integer is represented using length
bytes and defaults to 1
.
An OverflowError
is raised if the integer cannot be represented using the
given number of bytes.
my_bytes = (1024).to_bytes(2, byteorder='big') print(my_bytes) # ๐๏ธ b'\x04\x00' my_bytes = (1024).to_bytes(10, byteorder='big') print(my_bytes) # ๐๏ธ b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
The second argument we passed to the to_bytes()
method determines the byte
order that is used to represent the integer.
The byteorder
argument defaults to big
.
If the byteorder
argument is set to big
, then the most significant byte is
at the beginning of the byte array.
If you set the byteorder
argument to little
, the most significant byte is at
the end of the byte array.
my_bytes = (1024).to_bytes(2, byteorder='little') print(my_bytes) # ๐๏ธ b'\x00\x04' my_bytes = (1024).to_bytes(10, byteorder='little') print(my_bytes) # ๐๏ธ b'\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00'
You can also create a reusable function that converts integers to bytes.
def int_to_bytes(integer): return integer.to_bytes((integer.bit_length() + 7) // 8, 'big') print(int_to_bytes(65)) # ๐๏ธ b'A' print(int_to_bytes(1024)) # ๐๏ธ b'\x04\x00' print(int_to_bytes(2048)) # ๐๏ธ b'\x08\x00'
The int_to_bytes
function takes an integer as a parameter and converts it to a
bytes object.
Conversely, if you need to convert a bytes
object to an integer, use the
int.from_bytes()
method instead.
def int_from_bytes(bytes_obj): return int.from_bytes(bytes_obj, byteorder='big') print(int_from_bytes(b'A')) # ๐๏ธ b'A' print(int_from_bytes(b'\x04\x00')) # ๐๏ธ b'\x04\x00' print(int_from_bytes(b'\x08\x00')) # ๐๏ธ b'\x08\x00'
The int.from_bytes()
method returns the integer represented by the given byte
array.
The first argument the method takes must be a bytes-like object or an iterable that produces bytes.
The byteorder
argument determines which byte order is used to represent the
integer.
The default value is big
which means that the most significant byte is at the
beginning of the byte array.
If the byteorder
argument is set to "little"
, then the most significant byte
is at the end of the byte array.
The examples above only work for unsigned (non-negative integers).
If you need to convert signed integers to bytes, use the following function instead.
def int_to_bytes(integer): return integer.to_bytes( length=(8 + (integer + (integer < 0)).bit_length()) // 8, byteorder='big', signed=True ) print(int_to_bytes(-1024)) # ๐๏ธ b'\xfc\x00' print(int_to_bytes(-2048)) # ๐๏ธ b'\xf8\x00'
Calculating the length
argument when converting signed (negative) integers to
bytes is a bit more complicated.
The signed
argument determines whether two's complement is used to represent
the integer.
If signed
is False
and a negative integer is supplied, an OverflowError
is
raised.
By default, the signed
argument is set to False
.
The following function can be used if you need to convert signed bytes to integers.
def int_from_bytes(binary_data): return int.from_bytes(binary_data, byteorder='big', signed=True) print(int_from_bytes(b'\xfc\x00')) # -1024 print(int_from_bytes(b'\xf8\x00')) # -2048
The signed argument indicates whether two's complement is used to represent the integer.
If you need to convert the integer to a string and then bytes, use the str.encode method.
num = 2048 my_bytes = str(num).encode(encoding='utf-8') print(my_bytes) # ๐๏ธ b'2048'
We passed the integer to the str()
class to convert it to a string and then used the str.encode()
method to
convert the string to bytes.
The same can be achieved by using the bytes class.
num = 2048 my_bytes = bytes(str(num), encoding='utf-8') print(my_bytes) # ๐๏ธ b'2048'
If you need to convert the bytes object back to an integer, use the bytes.decode method and the int() class.
num = 2048 my_bytes = str(num).encode(encoding='utf-8') print(my_bytes) # ๐๏ธ b'2048' my_int = int(my_bytes.decode(encoding='utf-8')) print(my_int) # ๐๏ธ 2048
The bytes.decode()
method converts the bytes object to a string.
The last step is to use the int()
class to convert the string to an integer.
You can learn more about the related topics by checking out the following tutorials:
__main__
module in Path