A Decimal is term that describes the base-10 number system.
A Hexadecimal is a term that describes the base-16 number system.
Step 1: Write down the remaining value after multiplying the decimal number by 16.
Step 2: Divide the quotient by 16 and write down the remainder again.
Step 3: Repeat steps 1 and 2, until you get the quotient as 0 or greater than 0.
Step 4: Now, write the remainders in reverse order, starting with the last remainder and moving on to the others.
Step 5: Another way to read this is that the binary number’s Least Significant Bit (LSB) is at the top and its Most Significant Bit (MSB) is at the bottom.
The remainder will be from 0 to 15 because of divisor 16. Replace 10, 11, 12, 13, 14, and 15 with A, B, C, D, E, and F, respectively.
Below is a visualization to help you understand the above steps to convert decimal to binary.
Example: Covert decimal number into hexadecimal.
The following code shows the conversion from decimal to hexadecimal.
let n = 540;console.log(n.toString(16).toUpperCase());
Line 1: Declaring a variable n
.
Line 2: A message is written to the console using the console.log() function, and a number is converted to a string in uppercase letters using the n.toString(16) method, which uses base-16 to convert the number to a string.
Free Resources