• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Convert Integer to Bytes in Python

Convert Integer to Bytes in Python

October 14, 2022 Leave a Comment

To convert an integer to bytes in Python, you can use the Python int to_bytes() function. to_bytes converts an int object into an array of bytes representing that integer.

num = 10
num_bytes = num.to_bytes(3, byteorder="big")

print(num_bytes)

#Output:
b'\x00\x00\n'

When working with different types of objects when programming, the ability to easily be able to convert a variable into a variable of another type is useful.

One such case is if you want to convert an integer to bytes in Python.

Converting an integer to bytes in Python is easy. To convert integers to bytes, you can use the Python int to_bytes() function.

to_bytes converts an int object into an array of bytes representing that integer.

to_bytes takes in three arguments. The first argument is the number of bytes you want returned for representing the integer.

The second argument is the order of bytes. You can pass “little” or “big” to “byteorder”. The difference here is passing “little” will result in having the most significant byte at the end of the array, while “big” will result in having the most significant byte at the beginning of the array.

If you want to go the other way and convert a byte to an integer, this order will matter!

The third argument is used if you have a negative number and want to convert a negative number into bytes.

Below are some examples showing you how to convert integers to bytes with to_bytes() in Python.

num1 = 5
num2 = 10
num3 = -5

print(num1.to_bytes(1, byteorder="big"))
print(num1.to_bytes(2, byteorder="big"))
print(num1.to_bytes(3, byteorder="little"))
print(num2.to_bytes(1, byteorder="big"))
print(num2.to_bytes(2, byteorder="big"))
print(num2.to_bytes(3, byteorder="big"))
print(num3.to_bytes(1, byteorder="little", signed=True))

#Output:
b'\x05'
b'\x00\x05'
b'\x05\x00\x00'
b'\n'
b'\x00\n'
b'\x00\x00\n'
b'\xfb'

Hopefully this article has been useful for you to learn how to convert an integer to a bytes object in Python with to_bytes().

Other Articles You'll Also Like:

  • 1.  Python nth Root – Find nth Root of Number with math.pow() Function
  • 2.  PROC LIFETEST Equivalent in Python
  • 3.  Using Python to Convert Timestamp to Date
  • 4.  What Curly Brackets Used for in Python
  • 5.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 6.  Using Python to Print Degree Symbol
  • 7.  Using Python to Remove Quotes from String
  • 8.  Python ljust Function – Left Justify String Variable
  • 9.  Get Year from Date in pandas DataFrame
  • 10.  How to Write Pickle File to AWS S3 Bucket Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy