In this tutorial, we will look at how to convert an int type object to a bytes type object in Python with the help of some examples.
How to convert int to bytes in Python?
You can use the int
class method int.to_bytes()
to convert an int object to an array of bytes representing that integer. The following is the syntax –
# int to bytes int.to_bytes(length, byteorder, signed)
It takes the following arguments –
length
– The number of bytes to use to represent the integer. If the integer is not representable with the given number of bytes, anOverflowError
is raised.byteorder
– Determines the byte order used to represent the integer. Use'big'
as the byte order to have the most significant byte at the beginning of the byte array. Use'little'
as the byte order to have the most significant byte at the end of the byte array.signed
– Determines wheter to use two’s compliment to represent the integer. It is an optional parameter and isFalse
by default. Helpful in converting signed integers to bytes.
Examples
Let’s look at some examples of using the int.to_bytes()
function to convert an integer to bytes.
Using ‘big’ as the byteorder
Let’s convert the integer value 7
to a byte array of length 2 and with “big” as the byteorder.
# integer variable num = 7 # integer to bytes num_bytes = num.to_bytes(2, byteorder='big') # display result and type print(num_bytes) print(type(num_bytes))
Output:
b'\x00\x07' <class 'bytes'>
We get the returned value as bytes with the most significant byte at the beginning.
Using ‘little’ as the byteorder
Let’s use the same example as above but with “little” as the byteorder
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# integer variable num = 7 # integer to bytes num_bytes = num.to_bytes(2, byteorder='little') # display result and type print(num_bytes) print(type(num_bytes))
Output:
b'\x07\x00' <class 'bytes'>
We get the most significant byte at the end of the byte array.
Negative Integers to bytes with signed=True
If you use the default signed=False
on a negative integer, you will get an OverflowError
.
# integer variable num = -7 # integer to bytes num_bytes = num.to_bytes(2, byteorder='big') # display result and type print(num_bytes) print(type(num_bytes))
Output:
--------------------------------------------------------------------------- OverflowError Traceback (most recent call last) Input In [11], in <module> 2 num = -7 3 # integer to bytes ----> 4 num_bytes = num.to_bytes(2, byteorder='big') 5 # display result and type 6 print(num_bytes) OverflowError: can't convert negative int to unsigned
To convert negative integers to bytes with the int.to_bytes()
function, pass signed=True
. It will use two’s complement to represent the integer.
# integer variable num = -7 # integer to bytes num_bytes = num.to_bytes(2, byteorder='big', signed=True) # display result and type print(num_bytes) print(type(num_bytes))
Output:
b'\xff\xf9' <class 'bytes'>
We get the bytes for the negative integer.
For more on the int.to_bytes()
function, refer to its documentation.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.