How to Create Converter for Decimal to Binary
Submitted by alpha_luna on Thursday, August 18, 2016 - 17:09.
In the previous tutorial, we create a simple Converter For Binary To Decimal. Today, we create another converter this is an alternative of the previous tutorial. And it's called Converter for Decimal to Binary. This converter works by typing a numeric value from the user then the user clicks the "Ok" button to convert the Decimal Value to in Binary Form.
This program also creates using JavaScript. You can also check the live demo of this simple tutorial, so you can get an idea and you can try this out, let's start coding.
JavaScript Source Code
This script will ask the user to input the numerical value to convert decimal to binary.
Output:
The user input the numerical value to convert decimal to binary.
This is the output after the user gives the value to convert.
Hope that this simple yet useful tutorials that I created may help you to your future projects.
So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.
- <script>
- function decimalConvert_toBinary(dec,length){
- var output_result = "";
- while(length--)
- output_result += (dec >> length ) & 1;
- return output_result;
- }
- var input = parseInt(prompt("Enter a Number"));
- input_solve = decimalConvert_toBinary(input,8);
- if (!isNaN(input)) {
- document.write("<h2><b style='color:red;'>" +input + "<sub>10</sub></b> in decimal is equivalent to <b style='color:blue;'>"
- + input_solve + "<sub>2</sub></b> in binary. </h2>" );
- }
- while(isNaN(input)){
- input = parseInt(prompt("Please enter a numerical value."));
- input_solve =decimalConvert_toBinary(input,8);
- if (!isNaN(input)) {
- document.write("<h2><b style='color:red;'>" +input + "<sub>10</sub></b> in decimal is equivalent to <b style='color:blue;'>"
- + input_solve + "<sub>2</sub></b> in binary.</h2>" );
- }
- }
- </script>
Add new comment
- 8 views