Number to Bin Converter Using Javascript
Submitted by jaredgwapo on Tuesday, February 16, 2016 - 10:46.
Language
In this tutorial, I am going to teach you how to make a number to binary converter. You can also convert it to hexadecimal and octal representation.
To use this simple code just download the source code below.
Finally, you have created a simple converter of number to binary. For more suggestions, queries and questions, feel free to comment below.
Instructions
First, we will be writing our html code.
This will be our javascript code.
- <script type = "text/javascript">
- function dec2bin() {
- var x = document.getElementById("deci").value;
- if ((/[^0-9]/g.test(x)) || x == "") {
- alert ("You must enter an integer decimal number!");
- document.getElementById("deci").value = "";
- document.getElementById("deci").focus();
- return false;
- }
- x = parseInt(x);
- var bin = x.toString(2);
- var hex = x.toString(16).toUpperCase();
- var octal = x.toString(8);
- var figs = "The binary representation of " + x + " is " + bin + "<br>";
- figs = figs + "The hexadecimal representation of " + x + " is " + hex + "<br>";
- figs = figs + "The octal representation of " + x + " is " + octal + "<br>";
- document.getElementById("result").innerHTML = figs;
- }
- </script>
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 63 views