How to convert hex number to decimal in JavaScript

Aug 21, 2023#javascript#how-to

Hexadecimal (or hex for short) is a number system that uses 16 symbols to represent numbers. The symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. Each symbol corresponds to a value from 0 to 15. For example, A is 10, B is 11, and F is 15.

Hex numbers are often used in computer science and programming because they can represent binary numbers more compactly and conveniently. For example, the binary number 1010 0010 0011 can be written as A23 in hex.

Decimal is a number system that uses 10 symbols to represent numbers. The symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, corresponds to a value from 0 to 9.

To convert a hex number to a decimal number in JavaScript, you can use the parseInt() function.

parseInt(string)
parseInt(string, radix)

This function takes two arguments: a string and a radix. The string is the hex number you want to convert, and the radix is the base of the number system you want to convert to. In this case, the radix is 16 for hex and 10 for decimal.

For example, if you want to convert the hex number FF to decimal, you can write:

console.log(parseInt("FF", 16)); // 255

Because FF in hex is equal to (15βˆ—161)+(15βˆ—160)=(240)+(15)=255(15 * 16^1) + (15 * 16^0) = (240) + (15) = 255 in decimal.

Here are some more examples:

console.log(parseInt("2", 16)); // 2
console.log(parseInt("35", 16)); // 53
console.log(parseInt("1f4", 16)); // 500
console.log(parseInt("7b2", 16)); // 1970
console.log(parseInt("123abc", 16)); // 1194684

To convert a decimal number to a hex number, you can use the toString() method of the Number object. This method takes a parameter called radix, which specifies the base of the converted string. For hexadecimal, the radix is 16.

var decimalNumber = 255; 
var hexNumber = decimalNumber.toString(16); 
console.log(hexNumber); // ff

Math of converting hex to decimal

Let HH represent the hex number, and let dn,dnβˆ’1,…,d2,d1,d_{n}, d_{n-1}, \ldots, d_{2}, d_{1}, and d0d_{0} represent its individual digits from left to right.

The decimal equivalent (DD) of the hex number HH can be calculated using the formula:

D=dnΓ—16n+dnβˆ’1Γ—16nβˆ’1+…+d2Γ—162+d1Γ—161+d0Γ—160D = d_{n} \times 16^{n} + d_{n-1} \times 16^{n-1} + \ldots + d_{2} \times 16^{2} + d_{1} \times 16^{1} + d_{0} \times 16^{0}

In this formula:

  • nn is the number of digits in the hex number minus 1 (since the digits are indexed from left to right starting at 0).
  • dn,dnβˆ’1,…,d2,d1,d0d_{n}, d_{n-1}, \ldots, d_{2}, d_{1}, d_{0} are the individual digits of the hex number.
  • 16n,16nβˆ’1,…,162,161,16016^{n}, 16^{n-1}, \ldots, 16^{2}, 16^{1}, 16^{0} are the powers of 16 corresponding to the position of each digit.

So, the decimal equivalent is calculated by multiplying each digit by the corresponding power of 16 and summing up these products.

For example, to convert the hex number 1A1A to decimal:

  • n=1n = 1 (two digits, so n=1n = 1)
  • d1=1d_{1} = 1, d0=10d_{0} = 10 (A in hex is 10)
  • The calculation is: 1Γ—161+10Γ—160=16+10=261 \times 16^{1} + 10 \times 16^{0} = 16 + 10 = 26.

Math of converting decimal to hex

Converting a decimal number to hex involves dividing the decimal number by 16 repeatedly and keeping track of the remainders at each step. The remainders are then converted to their corresponding hex digits.

Let DD represent the decimal number to convert, and let RR represent the remainders obtained during the process.

  1. Rn=Dmod  16R_{n} = D \mod 16
  2. Qn=⌊D16βŒ‹Q_{n} = \left\lfloor \frac{D}{16} \right\rfloor
  3. Repeat steps 1 and 2 using the quotient QnQ_{n} as the new value of DD until QnQ_{n} becomes 0.
  4. The hex equivalent is obtained by reading the remainders from bottom to top. Convert each remainder to its corresponding hex digit.

For example, let’s convert the decimal number 305305 to hex:

  1. R1=305mod  16=1R_{1} = 305 \mod 16 = 1

  2. Q1=⌊30516βŒ‹=19Q_{1} = \left\lfloor \frac{305}{16} \right\rfloor = 19

  3. Continue the process:

    • R2=19mod  16=3R_{2} = 19 \mod 16 = 3
    • Q2=⌊1916βŒ‹=1Q_{2} = \left\lfloor \frac{19}{16} \right\rfloor = 1
    • R3=1mod  16=1R_{3} = 1 \mod 16 = 1
    • Q3=⌊116βŒ‹=0Q_{3} = \left\lfloor \frac{1}{16} \right\rfloor = 0
  4. Reading the remainders in reverse order: 131131.

So, the decimal number 305305 is equivalent to the hexadecimal number 131131.