Fix Python – Convert base-2 binary number string to int

Question

Asked By – Naftuli Kay

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?

Now we will see solution for issue: Convert base-2 binary number string to int


Answer

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 Python 2, and for Python 3.

This question is answered By – unwind

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0