0

I don't know much about Python, so sorry if this sounds silly. I am getting a string of 0s and 1s from an Arduino, and I need to convert it to a 'regular' number. I've found out how to convert a binary string to a decimal integer pretty easily, but how would I convert it if my binary string is stored in a variable ?

Let's say I've got a=00000110

int(a, 2) doesn't work (tells me "int() can't convert non-string with explicit base")

Is there a specific syntax that I should be aware of ? Can't I put a variable in here ?

Thanks in advance !

6
  • 3
    Try this with the appropriate input: a = "00000110". What you showed is trying to covert the integer 110, not a string. Commented Oct 24, 2016 at 21:39
  • First result from Google: Converting integer to binary in python Commented Oct 24, 2016 at 21:44
  • That's exactly what I was missing ! It's working now. Thanks ! Commented Oct 24, 2016 at 21:45
  • 1
    If you think you have 00000110 and it is not a string you have 72. Commented Oct 24, 2016 at 21:46
  • @PadraicCunningham So Python 2 is interpreting it as octal? Commented Oct 24, 2016 at 22:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.