Converting To/From Decimal

By now, we know how to convert about 16-or-so values between decimal and hexadecimal. To convert bigger numbers, here are some tricks we use.

Converting Decimal to Hex

Converting from decimal to hex involves a lot of division and remainders. If you’ve pushed long division out of your brain, wiki’s always there to help you brush up.

The steps to convert a number, let’s call it N, from decimal to hex look something like this:

Divide N by 16. The remainder of that division is the first (least-significant/right-most) digit of your hex number. Take the quotient (the result of the division) to the next step.

Note: if the remainder is 10, 11, 12, 13, 14, or 15, then that becomes the hex digit A, B, C, D, E, or F.

Divide the quotient from the last step by 16 again. The remainder of this division is the second digit of your hex value (second-from-the-right). Take the quotient from this division to the next step.

Divide the quotient from step 2 by 16 again. The remainder of this division is the third digit of your hex conversion. Noticing a pattern?

Keep dividing your quotient from the last step by 16, and storing the remainder until the result of a division is 0. The remainder of that division is your hex value’s left-most, most-significant digit.

Decimal-to-Hex Example: Convert 61453

Divide 61453 by 16. The result is a quotient of 3840, and a remainder of 13. That remainder becomes our first, right-most, least-significant hex digit – D. Take 3840 to the next step.

Now divide 3840 by 16. The resulting quotient is 240 with a remainder of 0. Our second hex digit is 0, and we take 240 to the next digit.

Divide 240 by 16, and you’ll get 15 with another 0 remainder. Our third hex digit is 0, and take 15 to step 4.

Finally, divide 15 by 16. That’ll produce the 0 quotient we’ve been waiting for, with a remainder of 15. That remainder means the hex digit for this position if F.

Finally, combine all four hex digits to create our hex value: 0xF00D.

Converting Hex to Decimal

There’s an ugly equation that rules over hex-to-decimal conversion:

There are a few important elements to this equation. Each of the h factors (hn, hn-1) is a single digit of the hex value. If our hex value is 0xF00D, for example, h0 is D, h1 and h2 are 0, and h3 is F.

Powers of 16 are a critical part of hexadecimal. More-signficant digits (those towards the left side of the number) are multiplied by larger powers of 16. The least-significant digit, h0, is multiplied by 160 (1). If a hex value is four digits long, the most-significant digit is multiplied by 163, or 4096.

To convert a hexadecimal number to decimal, you need to plug in values for each of the h factors in the equation above. Then multiply each digit by its respective power of 16, and add each product up. Our step-by-step approach is:

Start with the right-most digit of your hex value. Multiply it by 160, that is: multiply by 1. In other words, leave it be, but keep that value off to the side.