0

I want to convert a binary string (user input) to its base 10 equivalent value. I think I am somewhat close but just can't get it. This is what I have come up with so far. I need it to go through each individual number in the binary string and assign its base 10 equivalent value, then add all of those up for the entire base 10 number.

def getBaseTen(myString):
    x = myString[0]

    needAnswers = len(myString)
    n = len(myString)

    two = (2^n)


    if (needAnswers >= 1):
        return (int(x)*int(two))
wahlysadventures
  • 139
  • 1
  • 1
  • 8

2 Answers2

4

The built-in int() can do this with an optional argument for the base:

>>> a = '11010'
>>> int(a)
11010
>>> int(a, 2)
26
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
1

Something like this: int(x, base=2) Link for more on this.

def getBaseTen(myString):
    int(myString, 2)
nitishagar
  • 9,038
  • 3
  • 28
  • 40