Hex to Decimal and Decimal to Hex
Join the Discussion
A number of computer related calculations and values require that hexadecimal numbers be used in place of decimal numbers. Hexadecimal numbers use a number base of 16 instead of 10. This means that there are sixteen possible values for each hexadecimal digit. The letters A through F are used to represent 10 through 15 respectively after which '10' is used to represent 16 and so on.
To make your calculations easier you might find it useful to be able to convert between decimal and hexadecimal values. The following functions will do these conversions for you.
function h2d(h) {return parseInt(h,16);}
The d2h function takes a decimal value and returns the hexadecimal equivalent while the h2d function does the opposite, for example d2h(511) will return '1FF' and h2d('FFFF') will return 65535.
Neither of these functions is very long or complicated since each uses functionality built into Javascript itself. Neither is all that obvious though unless you happen to know what what they actually do and since they are not directly related to one another except in that they apply the opposite functionality to one another creating functions like this should make your code more readable.

