Convert base-2 binary number string to int

376

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?

This question is tagged with python

~ Asked on 2012-01-19 15:01:45

The Best Answer is


674

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.

~ Answered on 2012-01-19 15:02:39


40

Just type 0b11111111 in python interactive interface:

>>> 0b11111111
    255

~ Answered on 2015-01-27 04:00:03


Most Viewed Questions: