Re: A86: HEX Conversion


[Prev][Next][Index][Thread]

Re: A86: HEX Conversion




In a message dated 12/11/98 10:56:58 PM Pacific Standard Time,
uneven14@hotmail.com writes:

> About 2 weeks ago I asked if someone could explain how to convert HEX 
>  numbers to decimal numbers.  Someone posted a routine to do this however 
>  what I wanted is how you actually do it.  Some sort of explanation on 
>  how to look at 3f and say "hmm thats 13" (or whatever that may be).  I 
>  know 1=1 (duh) 2=2 ... a=11 b=12 (right?) but how do you use the two 
>  together?  Is it the first times the second?  The first plus the second?  
>  If someone could explain this (NOT write a routine) I'd be thankful.

It's easier to explain hexadecimal when you understand binary, as they are
based on the same concept. Binary is easier to understand, at first. Hex and
bin are both number systems based on different bases. Binary is base 2 and
hexadecimal is base 16. It's a fairly easy concept, and knowing it will make
you look smart anywhere. :)
(If you have trouble seeing the diagrams, copy this into a program that will
display it in fixed-width font, like Assembly Studio.)
All numbers will use the conventional TASM prefixes: binary numbers begin with
a % (%1011), hexadecimal numbers begin with a $ ($FC00) and decimal numbers
(the number system you're familiar with) is displayed normally (56).

Decimal numbers use a base 10 number system. If you recall from way back when
in second grade, you might have seen charts that look like this:

| 10,000 | 1,000 | 100 | 10 | 1 |
|   3    |   4   |  6  |  1 | 8 |

This would represent the number 34,618. However, since we are all grown up
now, we understand powers. The number 34,618 could also be put in this chart:

| 10^4 | 10^3 | 10^2 | 10^1 | 10^0 |
|  3   |  4   |  6   |  1   |  8   |

10^4 * 3 = 30000
10^3 * 4 =  4000
10^2 * 6 =   600
10^1 * 1 =    10
10^0 * 8 =     8
       Sum:34618

Decimal is also called a Base 10 number system. Do you see why? The base of
the powers in each place is a 10, and each section of the chart represents 10
to an increasing power. Binary works on the same principal. Instead of having
10 as the base number, the number 2 is used. That means that there are only
two digits used. In binary, these digits are 1 and 0. Numerals 2-9 do not
exist. Let's say that someone gave us the number %101101. How would we know
what it is? We could use one of those chart thingies:

| 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |
|  1  |  0  |  1  |  1  |  0  |  1  |

2^5 * 1 = 32
2^4 * 0 =  0
2^3 * 1 =  8
2^2 * 1 =  4
2^1 * 0 =  0
2^0 * 1 =  1
      Sum:45

So we see that %101101 is 45. Cool or what? Now it gets a little more
complicated. In binary, there are less possible numbers. In hexadecimal, more
numbers are added. After 0-9, there is A, B, C, D, E, and F. They stand for
10, 11, 12, 13, 14, and 15, respectively. If we were to have the hex number
$C62A, we would figure it out this way:

| 16^3 | 16^2 | 16^1 | 16^0 |
|  C   |  6   |  2   |  A   |

16^3 * C = 49152  (tricky one! 16^3=4096 times 12, which is C)
16^2 * 6 =  1536
16^1 * 2 =    32
16^0 * A =    10
       Sum:50730

Fourtunately, you'll probably never have to figure out numbers that high. I
had to redo my calculations several times to make sure I was right on that
last one. :) More likely, you'll need to know what $C6 means or something.
That's easy:

| 16^1 | 16^0 |
|  C   |  6   |

16^1 * C = 192
16^0 * 6 =   6
      Sum: 198

Not too difficult? That's probably one of the most complicated one that you'll
ever need to do in real programming. It also depends on your programming
style. If you have any more questions, feel free to ask! (I'm thinking about
writing this up as an actual tutorial.)