28

Possible Duplicate:
Convert binary string to int

How would I convert this binary value '101011111' to decimal form in Python?

The function converts binary numbers into decimal numbers.

Inputs: string b: a binary number

Outputs: int d: a decimal representation of b

def Binary_to_Decimal(b):
    #what needs to be filled in
    return d
Community
  • 1
  • 1
Aaron Porter
  • 325
  • 1
  • 4
  • 7

1 Answers1

124

You can use int casting which allows the base specification.

int(b, 2)  # Convert a binary string to a decimal int.
Dennis
  • 56,821
  • 26
  • 143
  • 139
Benjamin Powers
  • 3,186
  • 2
  • 18
  • 23