[Tutor] Binary to Decimal conversion

bob gailer bgailer at gmail.com
Tue Mar 10 01:42:04 CET 2009


Chris Castillo wrote:
> I am having some difficulties in producing the correct code for a 
> simple binary to decimal conversion program. The arithmetic I want to 
> use is the doubling method - so if I wanted to get the decimal 
> equivalent of 1001, I would start with multiplying 0 * 2 and adding 
> the left most digit. I would then take the sum of 1 and multiply it by 
> 2 and add the next digit to that product and so on and so forth until 
> I come to an end sum of 9.
>
> I seem to come up with something like this:
>
> binnum = raw_input("Please enter a binary number:  ")
>
>
> for i in range(0, len(binum), 1):
>     item = "0"
>     if i < len(binum) - 1:
>         item = binum[i + 1]
>    
>     binsum = binsum * int(item) * 2 + binsum + int(binum[i])
>
>
> print "\nThe binary number ", binum, " you entered converts to", 
> binsum, " in decimal."
>
>
> I can't really figure out what is going wrong here. 

I don't see anything going wrong. All I see is some Python code.

Would you be kind enough to tell us why you think something is going wrong.

If there is an error post the traceback

If you get a result you did not expect tell us what the input was, the 
desired result and the actual result.

-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list