Sunday 11 March 2012

Hexadecimal Conversion



If you work with computer programming or computer engineering (or computer graphics, about which more later), you will encounter base-sixteen, or hexadecimal, math.
Decimal math does not have one single solitary digit that represents the value of "ten". Instead, we use two digits, a 1 and a 0: "10". But in hexadecimal math, the columns stand for multiples of sixteen! That is, the first column stands for how many units you have, the second column stands for how many sixteens, the third column stands for how many two hundred fifty-sixes (sixteen-times-sixteens), and so forth.

In base ten, we had digits 0 through 9. In base eight, we had digits 0 through 7. In base 4, we had digits 0 through 3. In any base system, you will have digits 0 through one-less-than-your-base. This means that, in hexadecimal, we need to have "digits" 0 through 15. To do this, we would need single solitary digits that stand for the values of "ten", "eleven", "twelve", "thirteen", "fourteen", and "fifteen". But we don't. So, instead, we use letters. That is, counting in hexadecimal, the sixteen "numerals" are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

In other words, A is "ten" in "regular" numbers, B is "eleven", C is "twelve", D is "thirteen", E is "fourteen", and "F" is fifteen.  It is this use of letters for digits that makes hexadecimal numbers look so odd at first. But the conversions work in the usual manner.
Here is an example of the conversion:
  • Convert 35710 to the corresponding hexadecimal number.
Here, I will divide repeatedly by 16, keeping track of the remainders as I go. (You might want to use some scratch paper for this.)

Reading off the digits, starting from the top and wrapping around the right-hand side, I see that 35710 = 16516.
  • Convert 16516 to the corresponding decimal number.
List the digits, and count them off from the RIGHT, starting with zero:
digits:  
1  6   5
numbering:  
2  1   0


Remember that each digit in the hexadecimal number represents how many copies you need of that power of sixteen, and convert the number to decimal:

1×162 + 6×161 + 5×160
     = 1×256 + 6×16 + 5×1
     = 256 + 96 + 5
     = 357
Then 16516 = 35710.
  • Convert 6393310 to the corresponding hexadecimal number.
I will divide repeatedly by 16, keeping track of my remainders:



From the long division, I can see that the hexadecimal number will have a "fifteen" in the sixteen-cubeds column, a "nine" in the sixteen-squareds column, an "eleven" in the sixteens column, and a "thirteen" in the ones column. But I cannot write the hexadecimal number as "1591113", because this would be confusing and imprecise. So I will use the letters for the "digits" that are otherwise too large, letting "F" stand in for "fifteen", "B" stand in for "eleven", and "D" stand in for "thirteen".   Copyright © Elizabeth Stapel 1999-2011 All Rights Reserved
Then 6393310 = F9BD16.

  • Convert F9BD to decimal notation.
I will list out the digits, and count them off from the RIGHT, starting at zero:
digits:  
F  9   B  D
numbering:  
3  2    1  0
Actually, it will probably be helpful to redo this, converting the alphabetic hexadecimal "digits" to their corresponding "regular" decimal values:
digits:  
15    9  11  13
numbering:  
  3    2   1    0
Now I'll do the multiplication and addition:
15×163 + 9×162 + 11×161 + 13×160
      = 15×4096 + 9×256 + 11×16 + 13×1
      = 61440 + 2304 + 176 + 13
      = 63933
As expected, F9BD = 6393310.

By: Kirk Macaraeg



No comments:

Post a Comment