> Phython code to convert binary to decimal?

Phython code to convert binary to decimal?

Posted at: 2014-12-18 
I found a solution here (it's shows tabs better than yahoo):

http://www.simplestsolutions.net/binary-...

Here's their code:

#get the input

binaryString = str(input("Please enter a binary number"))

#if you want error checking this is where to put it

indexCounter = len(binaryString)-1 #starts at the end of the string

power = 0 #used to determine what power to raise to

sum = 0 #contains the total so far

#loop from the end of the input down to the start

while (indexCounter >= 0):

...sum = sum + pow(2,power)*int(binaryString[indexCount...

...indexCounter = indexCounter - 1

...power = power + 1

#Output the results:

print(binaryString,"as binary =",sum,"as decimal")

Is this the sort of thing you're after?

#!/usr/bin/python

#Bin2Dec.py

import math

binaryStr = raw_input("Enter binary number: ")

length = len(binaryStr)

decimal = 0

for i in range(length):

....if binaryStr[length-1-i] == "1":

.... ....decimal += math.pow(2,i)



print binaryStr, "in binary =", decimal, "in decimal."

#Note: I've used "...." to indicate tabs, which Y!Answers removes.

If you enter 100, have you entered one hundred or have you entered 4 in binary?

Are you going to enter a number 100 or a character string "100"?

Are you using Python 2.7 or Python 3.4?