Convert Bytes to Int in Python

In this post, we will see how to convert Bytes to Int in Python.

Normally, the data is stored in the storage in bytes format. In this article, we will discuss how we can convert data given as bytes to integer or int data types in python. For this, we will use the int.from_bytes() method.

How to Convert Bytes to Int in Python?

We will convert bytes to int using the int.from_bytes() method. The syntax for the from_bytes() method is as follows.

Here,

  • The parameter bytes takes the bytes object that has to be converted to int as the input argument. 
  • The byteorder parameter is used to define the byte order of the bytes object that is passed as the first input argument. 
    • If “big” is given as an input argument to the byteorder parameter, the first byte from the beginning of the bytes object is considered to be the most significant byte. 
    • If “little” is given as an input argument to the byteorder parameter, the first byte from the end of the bytes object is considered to be the most significant byte. 
    • To use the byte order of the machine on which the program is being run, you can use the sys.byteorder attribute using the sys module.
  • The parameter signed is used to determine if the integer being created is signed or an unsigned integer. By default, it has the value False denoting that the created integer will be unsigned.

How to Convert Bytes to Signed Int in Python?

To convert the bytes object to a signed int, we will set the parameter signed to True in the from_bytes() method. To observe this, let us first create a bytes object from an integer using the int.to_bytes() method. The to_bytes() method takes the integer value as the first input argument, the length of the output bytes object as the second input argument and the byteorder as the third input argument. After execution, it returns the bytes object. Then we can convert the bytes object to int using the from_bytes() method as shown below.

Output:

In the above example, we have first created a bytes object of length 5. After that, we have converted the bytes object to int. In the bytes object, you can observe the position of the {. It is at the right end of the bytes object when we create the bytes object with the ‘big’ byteorder. On the other hand, it is at the left end of the bytes object when we create the bytes object using the little‘ byteorder.

You should keep in mind that the byteorder specified in the from_bytes() method should be the same as the byteorder specified while creating the bytes object. Otherwise, the int object created from the bytes object doesn’t produce the desired output. You can observe this in the following example.

Output:

Here, you can observe that output shows garbage values when we use different byteorder values at the time of the creation of the bytes object and the int object.

How to Convert Bytes to Unsigned Int in Python?

To convert bytes to unsigned int in python, we can set the parameter signed to False or leave it empty in the from_bytes() method. You can observe this in the following example.

Output:

Again, you should use the correct byteorder in the from_bytes() method so that the expected output can be produced.

Conclusion

In this article, we have discussed how we can convert bytes to int in python. I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Was this post helpful?

Leave a Reply

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