How to Convert Binary to Int in Python?

The binary and decimals values or integers are frequently used by programmers while programming or performing real-word mathematical tasks. The binary values are defined by “base 2”, and the decimals values are defined by “base 10”.

Sometimes we need to perform mathematical calculations; the binary values must be converted to integer/decimal values. So to convert the binary to an integer, different methods are used in Python, such as int(), f-string, etc.

In this Python blog, you’ll learn how to convert binary to an integer using different examples:

Method 1: Using int() Function

The “int()” function is used in Python to convert any numerical input value into an integer. Let’s understand how we can utilize the “int()” function to convert binary to int by the following examples:

Example 1: Convert Binary to Int in Python

In the code given below, the “int()” function is used to convert the given binary number into the desired integer by setting the base “2”.

Code:

binary_num = "1111"
int_value = int(binary_num, 2)
print(int_value)

In the above code:

  • The binary number “1111” is initialized and stored in the variable “binary_num”.
  • The “int()” function takes the variable “binary_num” and base value “2” as an argument to convert the binary into an integer.

Output:

The binary value “1111” has been successfully converted into an integer “15” using the “int()” function.

Example 2: Convert Binary With “0b” Prefix to Int in Python

In the code below, the binary value starts with the prefix “0b”. The “int() ” function converts this binary value into an int. Here is an example:

Code:

binary_num = "0b1111"
int_value = int(binary_num, 2)
print(int_value)

In the above code:

  • The “int()” function accepts the binary value as an argument and converts it into an integer by setting the base “2”.

Output:

The above output shows that the binary value “1111” has been successfully converted into an integer “15” using the “int()” function.

Method 2: Using bitstring Module

To deal with binary data in a simple and organized way, the “bitstring” module is used in Python. The bitstring module is an open-source module that can be installed using the following command:

> pip install bitstring

The below snippet shows that the “bitstring” module has been successfully installed in our system.

Code:

from bitstring import BitArray
binary_num = "0111"
int_value = BitArray(bin=binary_num).int
print('Positive Integer Value:', int_value)

#For Negative Value Use '1' at the Most Significant Bit
binary_num = "1111"
int_value = BitArray(bin=binary_num).int
print('Negative Integer Value: ', int_value)

In the above code:

  • The “BitArray()” function is used to convert the input binary number into an integer. The above code is divided into two parts.
  • In the first part, the binary number represents a positive number because the MSB (Most Significant Bit) is “0”.
  • In the second part, the MSB is “1”, which represents that the converted number will be a negative integer.

Output:

The “BitArray()” function converts the given binary value to integers based on the MSB.

Method 3: Using f-string

The “f-string” method is used in Python to format the string. This method is also used to convert the given binary number to an integer in Python. Here is an example:

Code:

int_value = f'{0b1111:#0}'
print('Integer Value:', int_value)

In the above code:

  • The binary value “1111” is specified using the binary prefix “0b”.
  • The “#0” is used to convert the given binary value to an integer value.

Output:

The binary value “0b1111” has been converted into a string using the “f-string” method.

Conclusion

To convert binary to integer, the “int()” function, the “bitstring” module, and the “f-string” formatting method are used in Python. The “int()” function accepts the binary and base values as arguments and returns the integer value. Similarly, the “bitstring” module function and the “f-string” method are utilized in Python to convert the given binary into an integer. This Python post discussed various methods to convert binary to int in Python.