binary to decimal conversion

Adrian Eyre a.eyre at optichrome.com
Mon Mar 20 08:55:53 EST 2000


>> 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?
>> [snip]

> Yes, use string.atoi
> >>> import string
> >>> string.atoi("110", 2)
> 6
> [snip]

...or in the latest CVS Python (what will become v1.6), you can use int():

Python 1.5.2+ (#0, Feb  1 2000, 13:53:38) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> int("101010", 2)
42

I-always-thought-there-was-something-fundamentally-wrong-with-the-universe-l
y
y'rs

-----------------------------------------------------------------
Adrian Eyre <a.eyre at optichrome.com> - http://www.optichrome.com





More information about the Python-list mailing list