binary to decimal conversion
Joshua Macy
amused at webamused.com
Sat Mar 18 16:18:49 EST 2000
Michael Esveldt wrote:
>
> This morning I wrote a function that takes a binary number as a string
> and converts it to an integer. There must be a way to optimize this
> function, anyone have some tips?
>
> def btoi(num):
> output = 0
> col = range(len(num))
> col.reverse()
> for bit in range(len(num)):
> output = output + pow(2,col[bit])*int(num[bit])
> return output
>
> Thanks,
> Michael
> _______________________________________________________________
> dante at oz.net - Michael Esveldt - #FightThePower on Openprojects
Yes, use string.atoi
>>> import string
>>> string.atoi("110", 2)
6
>>> string.atoi("110110", 2)
54
Joshua
More information about the Python-list
mailing list