Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Java Script Decimal to Hexidecimal Converter

Many numbers used with computers and the Web are specified in "hexadecimal" notation. This is because humans use the decimal numbering system and computers use the binary numbering system. The hexadecimal numbering system is a numbering system that makes it a little easier for humans to define binary numbers.

Whereas the decimal numbering system uses the characters 0 through 9 to get 10 values, When you add one to 9 you change the 9 to 0 and put a 1 in the ten's place. The hexadecimal numbering system uses the characters 0 through f to get 16 values. When you add 1 to hexidecimal F, you change the F to 0 and put 1 in the "sixteens" place.

It's not easy for most people to convert between decimal and hexadecimal in their head. In this article, I will provide you with Java Script code for a Decimal to Hexidecimal / Hexidecimal to Decimal Converter. I'll explain the code in detail so that you can have confidence modifying it for use on your website.

Decimal to Hexidecimal/Hexidecimal to Decimal Converter
Prefix hexidecimal numbers with 0x

The first thing you need is a form to enter the number you want to convert. Below is the html code for the form which you can paste into your webpage.

Decimal to Hexidecimal/Hexidecimal to Decimal Converter<br>
Prefix hexidecimal numbers with 0x<br>
<form name="hexform">
<input type="text" name="decnum" style="text-align:right"
  size="10" maxlength="10">
<input type="button" value="Convert" onclick="convert()">
</form>

This form has no "Submit" button because the form will not be submitted to the server. All the processing takes place on the client side. Instead, a generic button is provided. The onclick event of the button calls the Java Script convert function.

The forms text box has a style rule applied that causes the text to align with the right side of the text box so that it will look like a regular calculator. The "maxlength" attribute sets the largest number that can be entered into the text box to 10 digits.

Next you need the code for the Java Script convert function. Below is the Java Script code which you can paste into your webpage. You should paste the code into the <head> section of your webpage, but anywhere above the form code will work.

<script language="JavaScript">
function convert()
{
  var strIn = document.hexform.decnum.value;
  var strNum = strIn.indexOf("0x");

  if(strNum) // convert to hexadecimal
  {
    if(isNaN(strIn))
    {
      alert("Not a Number");
    }
    else
    {
      var num = parseInt(strIn);
      var hexnum = num.toString(16);
      document.hexform.decnum.value = "0x" + hexnum;
    }
  }
  else  // convert to decimal
  {
    if(isNaN(strIn))
    {
      alert("Not a Number");
    }
    else
    {
      var num = parseInt(strIn);
      document.hexform.decnum.value = num;
    }
  }
}
</script>

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268