Yup, binary to decimal was easy. Play with the algorithm, do a little pseudocode, poke at the code a little to figure out the Python syntax (reference manual!), debug a little and poof, it works.

*binary = input(“Enter a binary number.”)*
*lenBinary = len(binary)*
*dec = 0*
*for i in range(lenBinary): #Go through the length of the binary number.*
* if binary[lenBinary-(i+1)] == “1”: #Go from right to left checking if the value is a “1”.*
* dec = dec + 2**i #If a “1” convert to base 10 and add it up.*
*print(“The decimal equivalent of “, binary,” base 2 is “, dec, ” base 10.”)*

Undoubtedly the Python whizzes out there can snipe the heck out of it with syntax short cuts but this is what the kids will build initially. I do very few short cuts because they are often language specific. Of course now that I look at my code I think I can redo that range parameter to do some of what that if statement is doing…

There are some things in Python that really throw me off. I guess the biggest is the range object in the for statement. I keep forgetting the range is one less than the value. Of course I take a few minutes to remember that little detail. Like I said too many languages.

A comment to my last post by Bri Morrison suggested looking at Roman numeral conversions. I hate it when people make project suggestions like that. Especially good suggestions like that. Now I am going to sit and stare at the ceiling and have to figure out how to do it. As though I did not have enough things to stare at the ceiling about. So now, Roman numerals have no place value and a smaller value to the left of a bigger value means subtract…hmmm. What a strange way to have fun. Thank God I like to mountain bike and snowboard because otherwise I would live in front of this stupid computer.

### Like this:

Like Loading...

*Related*

This entry was posted on August 8, 2014 at 5:02 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

August 8, 2014 at 5:57 am |

That’s not binary to decimal. That’s binary as a string to binary internal representation. You’re using someone else’s binary to decimal converter (hidden in the print statement).

August 8, 2014 at 2:47 pm |

Hmmm. I do not see it. I do treat the binary number as a string so I can work on one place value at a time. If the value is a one compute the power of 2 for that place. Add them up as you go. This is the algorithm I would use to do it by hand, which was my objective. The print statement does no converting, it just prints out my two variables. I would definitely agree this is not an efficient way to do it or that it is the way it would be converted by mathematical software. I am trying to reinforce converting from a manual algorithm to a computer program and the power of 2 place values.

August 9, 2014 at 2:19 pm |

I think gasstationwithoutpumps is referring to the fact that you are printing the decimal number (which is stored internally in binary) to decimal. I.e., the computer is converting the internal binary to a decimal string representation. But yes, you’ve also already done the conversion as well :). Perhaps he wants you to convert to a decimal string??? I would give your solution an A.

August 10, 2014 at 2:25 am |

Consider me dense. I still do not see it. I have been programming at the kid level to long. How is the computer doing an internal conversion? “binary” and “dec” are just variables. “binary” is a string representation of a binary number, not a binary number such that the computer would recognize as binary. The algorithm just plucks it apart in the same manner as would be done by hand. If I were to do this with pencil and paper I would be doing the exact same process. What am I missing here?

August 10, 2014 at 4:58 am |

There are 3 different representations of numbers that are involved:

binary string

internal representation

decimal string

What you have done is “binary string”->”internal”.

You then used the “print” statement to invoke someone else’s “internal”->”decimal” conversion.

Usually, when people talk about binary->decimal conversion, they mean “internal”=>”decimal string”, not “binary string”=>internal, because computers haven’t used decimal representation internally since the early 1960s (except for calculators).