1

How do you convert a list of 0 and 1's into a binary number with base 10? For example: ([0,0,1,0,0,1]) will give me 9

1
  • 1
    Dup of lots of questions, including this recent one, which has a nice accepted answer. Commented Nov 30, 2013 at 5:34

3 Answers 3

8

try this:

int("".join(str(x) for x in a),2)

Convert the list into a string. And then make the binary to decimal conversion

Sign up to request clarification or add additional context in comments.

Comments

4

Well this is quite simple:

>>> a = [0,0,1,0,0,1]
>>> s = "".join(map(str, a))
>>> s
'001001'
>>> int(s, base=2)
9

Comments

0

You can use int function with the second parameter which indicates the base. As, the list is full of numbers, that has to be converted to a string (int accepts only string), so we do map(str, data)

print "".join(map(str, data))

Output

001001

So, it boils down to this

data = [0,0,1,0,0,1]
print int("".join(map(str, data)), 2)

Output

9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.