-1

I am trying to convert a string of 0s and 1s (bits) into an integer.

I've tryied this:

str(int(bin(stringofOandI)))

and I've tried this too:

str(int(bin(str(stringofOandI))))

but none of these works

It is supposed to convert this (just an example): in the EntryField : 01001101 in the console: 77

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • Possible duplicate of [Bytes to int - Python 3](https://stackoverflow.com/questions/34009653/bytes-to-int-python-3) – Alex_P Sep 19 '19 at 17:44
  • 1
    Please look at the above question. Maybe that can help you. Otherwise, try `ord()`. Also, please do a little bit of research before posting a question. I found the link instantly. – Alex_P Sep 19 '19 at 17:44
  • 2
    Possible duplicate of [Convert base-2 binary number string to int](https://stackoverflow.com/questions/8928240/convert-base-2-binary-number-string-to-int) – Kate Orlova Sep 19 '19 at 18:26

2 Answers2

1

In the int() function specify base 2:

b = '01001101'
i =   int(b, 2)
print(i)
mentallurg
  • 4,967
  • 5
  • 28
  • 36
0

If you want to convert a binary string (a string containing 0s and 1s), you need to use int, and you should pass 2 to the parameter base which by default is 10:

>>> stringofOandI = '01001101'
>>> int(stringofOandI, 2)
77
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228