# python – Convert base-2 binary number string to int

## The Question :

362 people think this question is useful

I’d simply like to convert a base-2 binary number string into an int, something like this:

>>> '11111111'.fromBinaryToInt()
255



Is there a way to do this in Python?

• While it doesn’t really matter, a binary string typically means a string containing actual binary data (a byte contains two hexadecimal digits, ie “\x00” is a null byte).
• Just to mention it: the other way around it goes like ‘{0:08b}’.format(65) (or f'{65:08b}’).

660 people think this answer is useful

You use the built-in int function, and pass it the base of the input number, i.e. 2 for a binary number:

>>> int('11111111', 2)
255



Here is documentation for python2, and for python3.

39 people think this answer is useful

Just type 0b11111111 in python interactive interface:

>>> 0b11111111
255



32 people think this answer is useful

Another way to do this is by using the bitstring module:

>>> from bitstring import BitArray
>>> b = BitArray(bin='11111111')
>>> b.uint
255



Note that the unsigned integer is different from the signed integer:

>>> b.int
-1



The bitstring module isn’t a requirement, but it has lots of performant methods for turning input into and from bits into other forms, as well as manipulating them.

8 people think this answer is useful

Using int with base is the right way to go. I used to do this before I found int takes base also. It is basically a reduce applied on a list comprehension of the primitive way of converting binary to decimal ( e.g. 110 = 2**0 * 0 + 2 ** 1 * 1 + 2 ** 2 * 1)

add = lambda x,y : x + y
reduce(add, [int(x) * 2 ** y for x, y in zip(list(binstr), range(len(binstr) - 1, -1, -1))])



4 people think this answer is useful

If you wanna know what is happening behind the scene, then here you go.

class Binary():
def __init__(self, binNumber):
self._binNumber = binNumber
self._binNumber = self._binNumber[::-1]
self._binNumber = list(self._binNumber)
self._x = [1]
self._count = 1
self._change = 2
self._amount = 0
print(self._ToNumber(self._binNumber))
def _ToNumber(self, number):
self._number = number
for i in range (1, len (self._number)):
self._total = self._count * self._change
self._count = self._total
self._x.append(self._count)
self._deep = zip(self._number, self._x)
for self._k, self._v in self._deep:
if self._k == '1':
self._amount += self._v
return self._amount
mo = Binary('101111110')



3 people think this answer is useful

A recursive Python implementation:

def int2bin(n):
return int2bin(n >> 1) + [n &amp; 1] if n > 1 else [1]



2 people think this answer is useful

If you are using python3.6 or later you can use f-string to do the conversion:

Binary to decimal:

>>> print(f'{0b1011010:#0}')
90

>>> bin_2_decimal = int(f'{0b1011010:#0}')
>>> bin_2_decimal
90



binary to octal hexa and etc.

>>> f'{0b1011010:#o}'
'0o132'  # octal

>>> f'{0b1011010:#x}'

>>> f'{0b1011010:#0}'
'90'     # decimal



Pay attention to 2 piece of information separated by colon.

In this way, you can convert between {binary, octal, hexadecimal, decimal} to {binary, octal, hexadecimal, decimal} by changing right side of colon[:]

:#b -> converts to binary
:#o -> converts to octal
:#0 -> converts to decimal as above example



Try changing left side of colon to have octal/hexadecimal/decimal.

def BitsToIntAFast(bits):