• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

2. Numbers and Dates > 2.6. Converting Between Decimal and Hexadecimal Numbers

Converting Between Decimal and Hexadecimal Numbers

NN 2, IE 3

Problem

You want to change a decimal number to its hexadecimal equivalent, and vice versa.

Solution

While the core JavaScript language provides facilities for going from hexadecimal to decimal, you need a custom function to go the other way.

To get a hexadecimal number as a string into its decimal equivalent, use the parseInt( ) method and specify the second parameter as 16:

var decimalVal = parseInt(myHexNumberValue, 16);

For myHexNumberValue, you can use either the hexadecimal characters for the number, or the format required for hexadecimal arithmetic in JavaScript: the hexadecimal characters preceded by 0x or 0X (a zero followed by an X). Here are some examples with string literals in the two formats:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint