The Wayback Machine - https://web.archive.org/web/20240526061411/https://www.geeksforgeeks.org/how-to-convert-bytes-to-int-in-python/
Open In App

How to Convert Bytes to Int in Python?

Last Updated : 17 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A bytes object can be converted to an integer value easily using Python. Python provides us various in-built methods like from_bytes() as well as classes to carry out this interconversion.

int.from_bytes() method

A byte value can be interchanged to an int value by using the int.from_bytes() method. This method requires at least Python 3.2 and has the following syntax : 

Syntax: int.from_bytes(bytes, byteorder, *, signed=False)

Parameters:

  • bytes – A byte object 
  • byteorder – Determines the order of representation of the integer value. byteorder can have values as either “little” where most significant bit is stored at the end and least at the beginning, or big, where MSB is stored at start and LSB at the end. Big byte order calculates the value of an integer in base 256. 
  • signed – Default value – False . Indicates whether to represent 2’s complement of a number. 

Returns – an int equivalent to the given byte

The following snippets indicate the conversion of byte to int object. 

Example 1:

Python3




# declaring byte value
byte_val = b'\x00\x01'
 
# converting to int
# byteorder is big where MSB is at start
int_val = int.from_bytes(byte_val, "big")
 
# printing int equivalent
print(int_val)


Output:

1

Example 2:

Python3




byte_val = b'\x00\x10'
 
int_val = int.from_bytes(byte_val, "little")
 
# printing int object
print(int_val)


Output:

4096

Example 3:

Python3




byte_val = b'\xfc\x00'
 
# 2's complement is enabled in big
# endian byte order format
int_val = int.from_bytes(byte_val, "big", signed="True")
 
# printing int object
print(int_val)


Output:

-1024


Similar Reads

How to Convert Int to Bytes in Python?
An int object can be used to represent the same value in the format of the byte. The integer represents a byte, is stored as an array with its most significant digit (MSB) stored at either the start or end of the array. Method 1: int.tobytes() An int value can be converted into bytes by using the method int.to_bytes(). The method is invoked on an i
2 min read
Find length of one array element in bytes and total bytes consumed by the elements in Numpy
In NumPy we can find the length of one array element in a byte with the help of itemsize . It will return the length of the array in integer. And in the numpy for calculating total bytes consumed by the elements with the help of nbytes. Syntax: array.itemsize Return :It will return length(int) of the array in bytes. Syntax: array.nbytes Return :It
1 min read
String to Int and Int to String in Python
Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting a string to int and int to string. Converting a string to an int If we want to convert a number that is represented in the string to int, we have to use the int() function. This function is used
2 min read
Python | Convert String to bytes
Inter conversions are as usual quite popular, but conversion between a string to bytes is more common these days due to the fact that for handling files or Machine Learning ( Pickle File ), we extensively require the strings to be converted to bytes. Let's discuss certain ways in which this can be performed. Method #1 : Using bytes(str, enc) String
5 min read
How to Convert Bytes to String in Python ?
In this article, we are going to cover various methods that can convert bytes to strings using Python. Convert bytes to a string Different ways to convert Bytes to string in Python: Using decode() methodUsing str() functionUsing codecs.decode() methodUsing map() without using the b prefixUsing pandas to convert bytes to strings Data types are the c
3 min read
Convert Bytes To Bits in Python
Among the must-have computer science and programming skills, having bit-level knowledge is one of those important things that you should know what it means to understand the data change in this big world. However, a potential need often arises for the conversion of bytes and bits in digital information into alternative units. In this article, we wi
2 min read
Convert Bytearray To Bytes In Python
In Python, dealing with binary data is a common task, and understanding how to convert between different binary representations is crucial. One such conversion is from a bytearray to bytes. In this article, we will explore five simple methods to achieve this conversion, along with code examples for each. Convert Bytearray To Bytes In PythonBelow, a
3 min read
Convert Unicode to Bytes in Python
Unicode, often known as the Universal Character Set, is a standard for text encoding. The primary objective of Unicode is to create a universal character set that can represent text in any language or writing system. Text characters from various writing systems are given distinctive representations by it. Convert Unicode to Byte in PythonBelow, are
2 min read
Convert Hex String to Bytes in Python
Hexadecimal strings are a common representation of binary data, especially in the realm of low-level programming and data manipulation. In Python, converting a hex string to bytes is a frequent task, and there are various methods to achieve this. In this article, we will explore some simple and commonly used methods for converting a hex string to b
3 min read
Convert a Bytes Array into Json Format in Python
In Python, dealing with bytes data is common, and converting a bytes array to JSON format is a frequent task. In this article, we will see how to convert a bytes array into JSON format in Python. Python Convert a Bytes Array into JSON FormatBelow are some of the ways by which we can convert a bytes array into JSON format in Python: Using decode and
3 min read
Article Tags :
Practice Tags :