.to_bytes() in Python Last Updated : 21 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In Python, the .to_bytes() method is used to convert an integer into its byte representation. This is useful when weneed to store or transmit data in binary format.Example: Convert the integer 10 into bytes Python num = 10 byte_data = num.to_bytes(2, 'big') print(byte_data) Outputb'\x00\n' Explanation:10: The integer being converted..to_bytes(2, 'big'): 2: Specifies the byte length (2 bytes). 'big': Most significant byte first (big-endian).b'\x00\n': The byte representation of 10 using big-endian format.Syntaxint.to_bytes(length, byteorder, *, signed=False)Parameters length: The number of bytes the integer should occupy.byteorder: The byte order used to represent the integer. It can be: 'big': Most significant byte first (big-endian). 'little': Least significant byte first (little-endian).signed: (Optional) If True, allows the representation of negative numbers. Default is False (unsigned).Return TypeReturns a bytes object representing the integer in the specified format.Examples of .to_bytes() method1. Using Little-Endian Python num = 10 byte_rep = num.to_bytes(2, 'little') print(byte_rep) Outputb'\n\x00' Explanation:10 in binary is 00001010, and in little-endian, it is represented as b'\n\x00'.The b'\n\x00' output shows that 10 is stored as \n (10 in hexadecimal) followed by \x00 (a zero byte).2. Representing Negative Numbers Python num = -10 byte_rep = num.to_bytes(2, 'big', signed=True) print(byte_rep) Outputb'\xff\xf6' Explanation:-10 in two's complement (signed) using big-endian is represented as b'\xff\xf6'.The b'\xff\xf6' output indicates that -10 is stored using two bytes where the first byte is 0xFF (representing the negative sign) and the second byte is 0xF6 (which is -10 in two's complement form).3. Converting bytes back to integer Python byte_data = b'\x00\x0a' num = int.from_bytes(byte_data, 'big') print(num) Output10 Explanation:b'\x00\x0a' represents the byte sequence where the first byte is 0x00 (0 in decimal) and the second byte is 0x0a (10 in decimal).Using big-endian byte order, this byte sequence is converted to the integer 10. Comment More infoAdvertise with us Next Article Append Data To Binary File In Python P pragatikheuvg Follow Improve Article Tags : Python Python Programs Practice Tags : python Similar Reads Convert String to bytes-Python The goal here is to convert a string into bytes in Python. This is essential for working with binary data or when encoding strings for storage or transmission. For example, given the string "Hello", these methods can convert it into a byte representation like b'Hello'. Letâs explore different method 2 min read Convert String to bytes-Python The goal here is to convert a string into bytes in Python. This is essential for working with binary data or when encoding strings for storage or transmission. For example, given the string "Hello", these methods can convert it into a byte representation like b'Hello'. Letâs explore different method 2 min read Append Data To Binary File In Python We are given a binary file and our task is to append data into that binary file in Python using different approaches. In this article, we will see how we can append data to binary file in Python. Append Data to Binary File in Python below, are the examples of Append Data To Binary File In Python: Ap 3 min read Append Data To Binary File In Python We are given a binary file and our task is to append data into that binary file in Python using different approaches. In this article, we will see how we can append data to binary file in Python. Append Data to Binary File in Python below, are the examples of Append Data To Binary File In Python: Ap 3 min read codecs.encode() in Python With the help of codecs.encode() method, we can encode the string into the binary form . Syntax : codecs.encode(string) Return : Return the encoded string. Example #1 : In this example we can see that by using codecs.encode() method, we are able to get the encoded string which can be in binary form 1 min read Python - Interconvert Tuple to Byte Integer Sometimes, while working with Python data, we can have a problem in which we need to perform conversion of tuple values, into combined byte and then to integer and vice-versa. This kind of problem can have application in data domains. Let's discuss certain ways in which this task can be performed. I 2 min read Like