.to_bytes() in Python Last Updated : 23 Jul, 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 info P pragatikheuvg Follow Improve Article Tags : Python Python Programs Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 5 min read Recursion in Python 6 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 5 min read Python Tuples 4 min read Dictionaries in Python 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like