[Tutor] Binary to Decimal conversion

Alan Gauld alan.gauld at btinternet.com
Tue Mar 10 22:32:13 CET 2009


"A.T.Hofkamp" <a.t.hofkamp at tue.nl> wrote

> If you reverse the computation, it gets even simpler:
> 
> binstr = raw_input("Please enter a binary number:  ")
> decnum = 0
> 
> for i in binstr:
>     decnum = decnum * 2 + int(i)
> 

But if we are allowed to use int() it is easier still:

decnum = int(raw_input("Please enter a binary number"), 2)

Since int() now converts binary strings to decimal... 

:-)

Alan G.



More information about the Tutor mailing list