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.
Welcome to Bucaro TecHelp!

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

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Calculators for Your Website : 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>

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Slide Show Code
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using Java Script
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269