Most of the manipulation of hex numbers you'll be performing will be simple conversions between hex and decimal, in both directions. The easiest way to perform such conversions is by way of a hex calculator, either a "real" calculator like the venerable TI Programmer (which I still have, wretched battery-eater that it is) or a software calculator with hexadecimal capabilities. (The old Sidekick TSR calculator for DOS was my constant companion in years past, because it did hexadecimal arithmetic so well.) Using a calculator demands nothing of your gray matter, of course, and won't help you understand the hexadecimal number system any better. So while you're a relatively green student, lay off anything that understands hex, be it hardware, software, or human associates.
In fact, the best tool is a simple four-function memory calculator. The conversion methods I describe here all make use of such a calculator since what I'm trying to teach you is number base conversion, not decimal addition or long division.
As you'll come to understand, converting hex numbers to decimal is a good deal easier than going the other way. The general method is to do what we've been doing all along in the number-dissection Figures 2.1, 2.2, and 2.3: Derive the value represented by each individual column in the hex number, and then add up the total of all the column values in decimal.
Let's try an easy one. The hex number is 7A2. Start at the right column. This is the units column in any number system. You have 2 units, so enter 2 into your calculator. Now store that 2 into memory. (Or press the SUM button, if you have a SUM button.)
So much for units. Keep in mind that what you're really doing is keeping a running tally of the values of the columns in the hex number. Move to the next column to the left. Remember that each column represents a value 16 times the value of the column to its right. So, the second column from the right is the 16s column. (Refer to Table 2.6 if you lose track of the column values.) The 16s column has an A in it. A in hex is decimal 10. The total value of that column, therefore, is 16 x 10, or 160. Perform that multiplication on your calculator, and add the product to the 2 that you stored in memory. (Again, the SUM button is a handy way to do this if your calculator has one.)
Remember what you're doing: evaluating each column in decimal and keeping a running total. Now, move to the third column from the right. This one contains a 7. The value of the third column is 16 × 16, or 256. Multiply 256 × 7 on your calculator, and add the product to your running total.
You're done. Retrieve the running total from your calculator memory. The total should be 1954, which is the decimal equivalent of 7A2H.
Okay—let's try it again, more quickly, with a little less natter and a much larger number: C6F0DBH.
First, evaluate the units column. B × 1 = 11 × 1 = 11. Start your running total with 11.
Evaluate the 16s column. D × 16 = 13 × 16 = 208. Add 208 to your running total.
Evaluate the 256s column. 0 × 256 = 0. Move on.
Evaluate the 4,096s column. F × 4,096 = 15 × 4,096 = 61,440. Add it to your running total.
Evaluate the 65,536s column. 6 × 65,536 = 393,216. Add it to the running total.
Evaluate the 1,048,576s column. C × 1,048,576 = 12 × 1,048,576 = 12,582,912. Add it to your total.
The running total should be 13,037,787.
Finally, do it yourself without any help for the following number: 1A55BEH.
The lights should be coming on about now. This is good, because going in the other direction, from our familiar decimal base 10 to hex, is much harder and involves more math. What we have to do is find the hex column values within a decimal number—and that involves some considerable use of that fifth-grade bogeyman, long division.
But let's get to it, again, starting with a fairly easy number: 449. The calculator will be handy with a vengeance. Tap in the number 449 and store it in the calculator's memory.
What we need to do first is find the largest hex column value that is contained in 449 at least once. Remember grade-school "gazintas"? (12 gazinta 855 how many times?) Division is often introduced to students as a way of finding out how many times some number is present in—"goes into"—another. It's something like that. Looking back at Table 2.6, we can see that 256 is the largest power of 16, and hence the largest hex column value, that is present in 449 at least once. (The next largest power of 16—512—is obviously too large to be present in 449.)
So, we start with 256, and determine how many times 256 "gazinta" 449: 449 / 256 = 1.7539. At least once, but not quite twice. So, 449 contains only one 256. Write down a 1 on paper. Don't enter it into your calculator. We're not keeping a running total here; if anything, we could say we're keeping a running remainder. The "1" is the leftmost hex digit of the hex value that is equivalent to decimal 449.
We know that there is only one 256 contained in 449. What we must do now is remove that 256 from the original number, now that we've "counted" it by writing a 1 down on paper. Subtract 256 from 449. Store the difference, 193, into memory.
The 256 column has been removed from the number we're converting. Now we move to the next column to the right, the 16s. How many 16s are contained in 193? 193 / 16 = 12.0625. This means the 16s column in the hex equivalent of 449 contains a…12? Hmmmm…remember the digit shortage, and the fact that in hex, the value we call 12 is represented by the letter C. From a hex perspective, we have found that the original number contains C in the 16s column. Write a C down to the right of your 1: 1C. So far, so good.
We've got the 16s column, so just as with the 256s, we have to remove the 16s from what's left of the original number. The total value of the 16s column is C × 16 = 12 × 16 = 192. Bring the 193 value out of your calculator's memory, and subtract 192 from it. A lonely little 1 is all that's left.
So we're down to the units column. There is one unit in one, obviously. Write that 1 down to the right of the C in our hexadecimal number: 1C1. Decimal 449 is equivalent to hex 1C1.
Now perhaps you'll begin to understand why programmers like hexadecimal calculators so much.
Let's glance back at the big picture of the decimal-to-hex conversion. We're looking for the hexadecimal columns hidden in the decimal value. We find the largest column contained in the decimal number, find that column's value, and subtract that value from the decimal number. Then we look for the next smallest hex column, and the next smallest, and so on, removing the value of each column from the decimal number as we go. In a sense, we're dividing the number by consecutively smaller powers of 16 and keeping a running remainder by removing each column as we tally it.
Let's try it again. The secret number is 988,664.
Find the largest column contained in 988,664 from Table 2.6: 65,536. 988,664 / 65,536 = 15 and change. Ignore the change. 15 = F in hex. Write down the F.
Remove F × 65,536 from 988,664. Store the remainder: 5,624
Move to the next smallest column. 5,624 / 4,096 = 1 and change. Write down the 1.
Remove 1 × 4,096 from the remainder: 5,624 − 4096 = 1528. Store the new remainder: 1,528.
Move to the next smallest column. 1,528 / 256 = 5 and change. Write down the 5.
Remove 5 × 256 from the stored remainder, 1,528. Store 248 as the new remainder.
Move to the next smallest column. 248 / 16 = 15 and change. 15 = F in hex. Write down the F.
Remove F × 16 from stored remainder, 248. The remainder, 8, is the number of units in the final column. Write down the 8.
There you have it: 988,664 decimal = F15F8H.
Note the presence of the H at the end of the hex number. From now on, every hex number in the text of this book will have that H affixed to its hindparts. It's important, because not every hex number contains letter digits. There is a 157H as surely as a 157 decimal, and the two are not the same number. (Quick, now: By how much are they different?) Don't forget that H in writing your assembler programs, as I'll be reminding you later on.
The best (actually, the only) way to get a gut feel for hex notation is to use it lots. Convert each of the following hex numbers to decimal. Lay each number out on the dissection table and identify how many 1s, how many 16s, how many 256s, how many 4,096s, and so on, are present in the number, and then add them up in decimal.
CCH 157H D8H BB29H 7AH 8177H A011H 99H 2B36H FACEH 8DB3H 9H
That done, now turn it inside out, and convert each of the following decimal numbers to hex. Remember the general method: From Table 2.6, choose the largest power of 16 that is less than the decimal number to be converted. Find out how many times that power of 16 is present in the decimal number, and write it down as the leftmost hex digit of the converted number. Then subtract the total value represented by that hex digit from the decimal number. Then repeat the process, using the next smallest power of 16 until you've subtracted the decimal number down to nothing.
39 413 22 67,349 6,992 41 1,117 44,919 12,331 124,217 91,198 307 112,374,777
(Extra credit for that last one…) If you need more practice, choose some decimal numbers and convert them to hex, and then convert them back.