October 17th, 2014
Converts from binary to decimal (and vice versa).
1) Enter a positive integer in the field below.
2) Select the base for the number entered.
3) Select the base to convert to.
4) Click “Convert” when ready.
In the near future, I shall upgrade this calculator so that it converts between more than two bases. Specifically, the new base converter will convert between any base from two to sixteen. Using the logic from the binary-decimal converter, you can convert from virtually any base to any base. It’s quite magical once you see the pattern! (To see a visual example of how to convert from binary to decimal, please see the video tutorial and blogpost here. This may come in handy if you’re new to binary numbers).
“The Dashboard” (User Interface)
The code below will generate the nice green-colored dashboard you see above. Copy and paste the code below into the body of your HTML file. Check to make sure it looks like the “applet” above by viewing the page through a web browser.
“The Push Lever” (Event Handler)
Here’s the “trigger” function that responds to button clicks:
“The Engine” (Functionality)
The event-handler function above acts like a “main” function in C/C++. We will use the buttonClick function to call the other “helper” functions. Some of these functions validate the user input, checking it for errors. We can think of those as our “scrubbers.” The converter functions represent the heart and soul of this machine. We can think of those as the “motor.”
The removeSpaces function eliminates any spaces from the textfield input, either typed accidentally or on purpose to bug me.
The validateInput function determines whether the input number and input base match. If they don’t match, the machine will display an error message. If the inputs do match, the machine will proceed with the appropriate calculation.
Below are the two most important functions in this application. These demonstrate the essential logic we need to convert between bases.
Here’s the function for converting from binary to decimal.
Here’s the function for converting from decimal to binary.