Number converter

Once in a while a man wants to convert a hexadecimal number to a decimal one, or even maybe a binary number to a decimal one. Google always helps if you ask “hex 2 bin”, but instead I decided to write an online application of my own.

The converter is implemented as very simple web page and can be found here. On the web page, on the fly conversions are possible between a decimal, hexadecimal and a binary number. So all conversions dec2hex, dec2bin, hex2dec, hex2bin, bin2dec and bin2hex are included in this basic form. See the complete junk of html and javascript code below.

<html>
<script type="text/javascript">
  function d2h(d) { return d.toString(16); };
  function h2d(h) { return parseInt(h,16); };
  function d2b(d) { return d.toString(2); };
  function b2d(b) { return parseInt(b,2); };

  function idec(d) {
    // pre: d is int
    document.cform.hex.value = d2h(parseInt(d));
    document.cform.bin.value = d2b(parseInt(d));
  };

  function ihex(h) {
  // pre: h is string
  var d = h2d(h);
  document.cform.dec.value = d;
  document.cform.bin.value = d2b(d);
  };

  function ibin(b) {
    // pre: b is string
    var d = b2d(b);
    document.cform.dec.value = d;
    document.cform.hex.value = d2h(d);
  };

</script>
<body>
<h1>Number converter</h1>
<p>Insert a decimal, hexadecimal or a binary number to convert it.</p>
<form name="cform">
Decimal: <input name="dec" onkeyup="idec(this.value)" />
Hexadecimal: <input name="hex" onkeyup="ihex(this.value)" />
Binary: <input name="bin" onkeyup="ibin(this.value)" />
</form>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *