Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Convert Bytes to Int in Python?

  • Last Updated : 23 Dec, 2020

A bytes object can be converted to an integer value easily using Python. Python provides us various in-built methds 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 : 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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


  • 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:


# 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



Example 2:


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



Example 3:


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



My Personal Notes arrow_drop_up
Recommended Articles
Page :