Number Conversions Using JavaScript

This program coverts Decimal number to binary, hexadecimal and octal.

Follow these Instructions or You can simply download source code below:

Html Code

 <b>Insert Decimal Number: </b><input type = "text" name = "deci" id = "deci" size = "15" maxlength = "15" />
 <input type="button" value="Convert!" onclick="dec2bin()" />
 <br><br>
 <div id = "result"></div>

JS 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>

CLICK THE BUTTON TO DOWNLOAD THE SOURCE CODE


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x