Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert Int to Bytes and Bytes to Int in Python

  • 04 Apr, 2025
  • Com 0
Converting Python Int to Bytes and Bytes to Integer

Integers represent whole numbers with arbitrary precision. Bytes represent immutable sequences of single bytes (0 – 255), which you can use for binary data.

If you are performing data serialization, network communication, cryptography, or writing binary files, you need to convert from one format to another.

Integer and Bytes representation in Python

Converting int to bytes

The most efficient way to convert an integer to bytes is to use the int.to_bytes() method.

int.to_bytes() method

Let’s convert an integer to a 2-byte big-endian format.

# Unsigned 16-bit big-endian
main_int = 1028

converted_byte = (main_int).to_bytes(2, 'big')  

print(converted_byte)
# Output: b'\x04\x04'

In the above code, we passed two arguments to the .to_bytes() method:

  1. 2: It is the number of bytes to use. Since the above input is a 16-bit number, it means 2 bytes.
  2. big: It means the most significant bytes will come first. If you pass ‘little’, it would mean the least significant byte comes first.

The final output is a byte string: b’\x04\x04′, which is a hex representation of bytes.

Let’s take another integer, 1029, and see the difference between big and little endians:

main_int = 1029

num_bytes = 2 # We want a 16-bit (2-byte) representation

big_endian_bytes = main_int.to_bytes(num_bytes, 'big')


little_endian_bytes = main_int.to_bytes(num_bytes, 'little')

# --- Print the results ---
print(f"Original Integer: {main_int}")
print("-" * 30)
print(f"Big-Endian Representation:    {big_endian_bytes}")
print(f"Little-Endian Representation: {little_endian_bytes}")
print("-" * 30)

# Optional: Print hex values for clarity
print(f"Big-Endian Hex:    0x{big_endian_bytes.hex()}")
print(f"Little-Endian Hex: 0x{little_endian_bytes.hex()}")

Output

Original Integer: 1029
------------------------------
Big-Endian Representation:    b'\x04\x05'
Little-Endian Representation: b'\x05\x04'
------------------------------
Big-Endian Hex:    0x0405
Little-Endian Hex: 0x0504

Negative numbers

If you are working with negative numbers, set the signed parameter to True.

main_int = -1028

converted_byte = (main_int).to_bytes(2, 'big', signed=True)

print(converted_byte)
# Output: b'\xfb\xfc'

Handling large integers

If your input integer size is very large, we can compute the minimum number of bytes required for unsigned integers.

n = 2**100

bytes_data = n.to_bytes((n.bit_length() + 7) // 8, 'big')

print(bytes_data)

# Output: b'\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

The above output shows that we presented a big number (2**100) in 13 bytes.

Converting bytes to int

int.from_bytes() method

You can convert back bytes to an integer optimally using the int.from_bytes() method. It accepts bytes that we want to convert back to an integer, byteorder, which can be big or little, and signed (optional argument), which is either True or False. By default, it is False.

bytes = b'\x04\x04'

# Convert bytes to int
int_converted = int.from_bytes(bytes, byteorder='big')

print(int_converted)
# Output: 1028

We obtain the converted integer 1028.

Choose between big-endian and little-endian based on your data format. That’s all!

Post Views: 324
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Check If a Tuple Contains an Element in Python
How to Print Without Newline in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend