Converting To and From Hexadecimal

Karl Matthes
5 min readAug 2, 2020

It occurred to me the other day that I was quite comfortable converting numbers from decimal to and from binary. But then I was trying to recall how you convert decimal numbers to and from hexadecimal, or simply “hex”, and then it occurred to me that I had totally forgotten how to do that. So, this article is going to be a refresher for myself, but hopefully it can help you too!

First, I want to establish this table, because I’m going to refer back to it a lot:

The first sixteen numbers in decimal and hex.

These first sixteen numbers are pretty important, and there’s no tricks to converting here; these should just be memorized. Hex is going to act just like any positional numeral system, which means once you hit the maximum number, and add one to it, you increment the number in the next position by one, and then set the maxed out number to zero. In decimal, this would be something like 9+1=10, or in hex, something like F+1=10. I bring this up because sometimes it’s good to think of the hex numbers as their decimal equivalents, and sometimes it’s better to think of them pure as hex. Or that’s to say, thinking of D as 13, or just letting D be D.

Converting to Hex from Decimal

Here’s the table I’ll use to visualize the steps for this conversion. For this example, I’ll be using the decimal number 8641.

I’ll go through the steps one column at a time first, but I’ll skip to row by row after the first one is done. First, we’ll divide 8641 by 16, which is 540.0625.

Next, we’ll take the decimal places, 0.0625, and multiply it by 16, which is 1. Alternatively, we could just use the remainder, but this works too.

It’ll be a little unnecessary here, but we’ll use the sixteen number decimal to hex table above to check what the hex equivalent of 1 is. Not super surprising, but it’s 1.

Finally, we’ll take everything before the decimal place in the first quotient, and make it the new decimal number to work with.

And from here, we can repeat until we hit zero. So, we’ll do 540/16=33.75. Then 0.75*16=12. And 12 is equal to C in hex.

The next number is 33, so 33/16=2.0625, 0.0625*16=1, and 1 is 1 in hex.

And finally, we’ll work with 2. Because 2 is smaller than 16, we know that it’ll be the remainder, but let’s keep to the process: 2/16=0.125, 0.125*16=2, and 2 is 2 in hex.

And we now have our answer in the “Hex” column. I’ll use decimal terms for this because there’s not really equivalent terminology in hex, but the first 1 will be in the one’s place, C will be in the ten’s place, the second 1 will be in the hundred’s place, and 2 will be in the thousand’s place, or written out as 21C1.

Converting to Decimal from Hex

Now, let’s reverse it, and we’ll use the same number, 21C1. We’ll do this by breaking up the number by positions, and multiplying them by some factor of 16. But first, I think a decimal example does help with this idea.

For this example, I’ll use 753. You could split up 753 into one number for each of its places and say that 753=700+50+3. Or if we break those into powers of 10, it could be 753=(7*10²)+(5*10¹)+(3*10⁰).

We can apply this same set-up to a hex number, but change some of the parts to decimal, and have it end up converting from hex to decimal. First, instead of varying powers of 10, we’ll use 16.

And that is 16 in decimal/base 10

We should also convert that C over to 12. And then we can do the same math as before.

(2*16³)+(1*16²)+(12*16¹)+(1*16⁰)=8641, which was our original number! We’ve come full circle! Each method is roughly the opposite of the other. In one direction, you’re dividing by 16, and in the other, you’re multiplying by 16. It should also be noted that nothing about either of these methods is specific to hexadecimal, and you could switch out the multiplication and division by 16 for any other number so that you convert to and from any other base. Heck, it’ll even work for binary:

13 is 1101 in binary.

And in reverse:

--

--