By John Selvia, ©2003

Ah, hexadecimal. If you've ever worked with colors in web page design, you've proably seen something like '<body bgcolor="#A09CF3"> or something to that effect. Somehow, that 6 digit hexadecimal number is equal to a lavender or light purple color. What on earth does 'A09CF3' mean? Before we explain that, let's look at what hexadecimal (hex) is.

Our decimal system, as mentioned in my Binary Tutorial, is a base-10 system, meaning we can count to any number in the universe using only 10 symbols or digits, 0 thru 9.

For the computer, a 10-based system probably isn't the most efficient system, so the computer uses binary (in reality, it uses microscopic switches which are either on or off, but we represent them using the digits '1' and '0'). Unfortunately, binary isn't very efficient for humans, so to sort of find a happy middle ground, programmers came up with hexadecimal.

Hexadecimal is a base-16 number format (hex=6, decimal=10). This means that instead of having only the digits from '0' to '9' to work with like our familiar decimal, or '1' and '0' like binary, we have the digits '0' to '15'. It also means that we are using the powers of 16, instead of the powers of 2 like in binary.

Digits '0' thru '9' are the same as our decimal system, but how can 10 thru 15 be digits? Well, since there are no 10 thru 15 symbols, we have to invent some.

To count beyond 9 in hexadecimal, we use A thru F of the alphabet:
DECIMAL
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HEXADECIMAL
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F


As soon as you count over 9 in hex, new digits take over. A=10, B=11, C=12, D=13, E=14 and F=15. Alright, so we have 6 new digits we never saw before. What can we do with them? Let's look at some samples-

$0F (Pronounced "OH EFF" or "ZERO EFF")

Note the use of the $ to show hex. You'll also see a hex number like this:

0x0F

In fact, the '0x' in front of a hex number is more current than the '$'. The '$' is sort of old school. Whether you use a '$' or a '0x', it tells you that you're working with a hex number, not a decimal one.Why use a special symbol to denote hex? Isn't it obvious that '0F' isn't an ordinary decimal number? Sure, but what if you had this number:

5396

Hmmm. How do you know whether that is a decimal 5396 or a hex 5396? See, not all hex numbers will have an 'A' thru 'F' in them. Many do, but not all. But as soon as we put a '$' or a '0x' in front of that number

$5396 or 0x5396

We know it's a hexadecimal number. Look, I'm already tired of typing it both ways, so I'm going to keep using the '$' to denote hex instead of showing you both methods from now on. If you feel more comfortable with the '0x', feel free to substitute that for the rest of the tutorial.

Here's another:

$A0FF (Pronounced "A OH EFF EFF" or "A ZERO EFF EFF")

And here's some others (yes, they're legitimate hex numbers):

(Numbers in parentheses are the decimal versions)
$BEAD (48,813) $DEAD (57,005) $FEED (65,261) $DEAF (57,007) $FADE (64,222) $BABE (47,806)
$ABBA (43,962) $DEED (57,069) $FACE (64,206) $BEEF (48,879) $CAD (3,245) $ADD (2,781)
$CAFE (51,966) $CAB (3,243) $BAD (2,989) $ACE (2,766) $FAD (4,013) $BED (3,053)

WHERE'S THE HEX??!?

Now, you may be wondering, "Hey, it's all well and good that you're showing us hex examples, but how do we go about translating those hex numbers into decimal?"

Well, I'll tell you... (and to keep things simple, we'll stick to smaller numbers for now):

Let's look at a typical small decimal number.

235

Now, in decimal, the above number is equivalent to:

2 x 100 + 3 x 10 + 5 or 200 + 30 + 5

Easy enough. Now look at a slightly larger decimal number:

1,236

This is equivalent to:

1 x 1000 + 2 x 100 + 3 x 10 + 6 or 1,000 + 200 + 30 + 6

Well, hex numbers follow a similar pattern where each digit is in a "place". But instead of 10's place or 100's place or 1000's place, hex uses the following progression (from right to left):

4096's Place
256's Place
16's Place
1's Place

Then, to translate a hex number such as $A0FF, you set up a chart like this:

4096
256
16
1
A
0
F
F

Remember that hex 'A' actually is equal to decimal 10, and hex 'F' is actually decimal 15 (look at the chart above if you just felt your brain drop into your underwear and are totally lost. I don't know where that image came from...). Here's our revised chart:

4096
256
16
1
10
0
15
15

Now multiply 10 times 4096, then 0 times 256, then 15 times 16, then 15 times 1. Then add the results (10 times 4096=40960 + 0 times 256=0 + 15 times 16=240 + 15 times 1=15 which comes out to decimal 41215.

SAY WHAT?!?!?

Still with me? No? Here's a smaller example for the 'larger example impaired':

2 Digit Hex Number: $B9
16's Place
1's Place
B
9

Remember that hex 'B' = 11 and hex '9' is still just 9:

16's Place
1's Place
11
9
Or
16
1
11
9

Since 'B' = 11, you multiply 11 times 16 which equals 176, then add a 9 times 1.

The final translation of $B9 to decimal would thus be 11 times 16 + 9 times 1=decimal 185.

Another 4 Digit Hex Number: $FEBC

4096
256
16
1
F
E
B
C

Remember that hex 'F' = 15, hex 'E' = 14, hex 'B'=11, and hex 'C' = 12:

4096
256
16
1
15
14
11
12

So multiply 15 times 4096, 14 times 256, 11 times 16, and 12 times 1 and add the results-

15 x 4096 =61440 + 14 x 256=3584 + 11 x 16=176 + 12 x 1 = 65212.

WHAT ABOUT REALLY LARGE HEX NUMBERS?!?!?

To translate (or convert as the big boys say) large hex numbers, you need to know more powers of 16. We've already seen the first four powers of 16: 4096 (16^3), 256 (16^2), 16 (16^1), and 1 (16^0). Whipping out my calculator (you think I studied multiplications tables in school larger than 12?!?!?), I find that after 4096 (16^3), the powers of 16 are:

16^7
16^6
16^5
16^4
16^3
16^2
16^1
16^0
268,435,456
16,777,216
1,048,576
65536
4096
256
16
1

Whew! Big numbers!!! So if we had a hex number like:

$FE973BDC

We could convert it by setting up the table like this:

268,435,456
16,777,216
1,048,576
65536
4096
256
16
1
F
E
9
7
3
B
D
C

This is going to be a big number. You'll definitely want your calculator for this one. Here goes:

Convert the hex digits to decimal digits using the chart at the top:

268,435,456
16,777,216
1,048,576
65536
4096
256
16
1
15
14
9
7
3
11
13
12

Multiply the decimal value in the bottom box by the powers of 16 values in the top box:

15 x 268,435,456 + 14 x 16,777,216 + 9 x 1,048,576 + 7 x 65,536 + 3 x 4,096 + 11 x 256 + 13 x 16 + 12 x 1 =

4,026,531,840 + 234,881,024 + 9,437,184 + 458,752 + 12,288 + 2,816 + 208 + 12 =4,271,324,124.

Ow! That hurts my brain. I'm resting for awhile...

HEXADECIMAL AND BINARY'S RELATIONSHIP

Alright, I'm back.

There is a wonderful (if you're a geek like me) relationship between hexadecimal and binary that might not be readily apparent. In fact, they're so closely tied together, that many programmers learn both equally well (especially assembler programmers). Let's take a look.

Here's a binary number (again, review my Binary Tutorial if you're a bit rusty. Get it? A 'bit' rusty. I kill me):

#10111101
Notice the pound sign to signify that it is a binary number so we don't confuse decimal 10,111,101 with binary 10111101.

So, based on our tutorial, we set up something like this first:

128
64
32
16
8
4
2
1
1
0
1
1
1
1
0
1

Now, add together all the numbers in the top boxes that have a 1 below them and ignore the numbers with zeroes below them.

128+32+16+8+4+1=189 in decimal.

Now if you split that little 8-bit binary number into 2 sets of 4 bits, let's call them the leftmost 4 bits and the rightmost 4 bits, we get

8
4
2
1
 
8
4
2
1
1
0
1
1
 
1
1
0
1

What the?!? Notice, we dumped the 128, 64, 32 and 16, because we're now working with 2 sets of 4 bit numbers instead of 1 big 8-bit number.

Wow that makes it so much easier...

To convert our binary number to hex, figure out what the leftmost 4 bits is equal to, and the rightmost 4 bits.

LEFTMOST
1011
=8+2+1
=11
=$B
RIGHTMOST
1101
=8+4+1
=13
=$D

And since 11 decimal = $B hex and 13 decimal = $D hex, the final hexidecimal conversion of binary #10111101 = $BD.

For larger binary numbers, it still works. Take this 16 bit number:

#1011010010100111

Break it up into four 4-bit sets:

8
4
2
1
8
4
2
1
8
4
2
1
8
4
2
1
1
0
1
1
0
1
0
0
1
0
1
0
0
1
1
1

And add up the numbers which have a 1 below them, but be sure to keep it in 4 different sets:

8+0+2+1
0+4+0+0
8+0+2+0
0+4+2+1
11
4
10
7
B
4
A
7

So #1011010010100111 is equal to $B4A7 hex and 46247 decimal.

Here's a 32-bit binary number to convert to hex:

#11010100010100111111011100010101

Break it up into eight 4-bit chunks...

8
4
2
1
8
4
2
1
8
4
2
1
8
4
2
1
8
4
2
1
8
4
2
1
8
4
2
1
8
4
2
1
1
1
0
1
0
1
0
0
0
1
0
1
0
0
1
1
1
1
1
1
0
1
1
1
0
0
0
1
0
1
0
1

Add up all numbers that have a 1 below them:

8+4+0+1
0+4+0+0
0+4+0+1
0+0+2+1
8+4+2+1
0+4+2+1
0+0+0+1
0+4+0+1
13
4
5
3
15
7
1
5
D
4
5
3
F
7
1
5

So binary 11010100010100111111011100010101 is equal to $D453F715 hex. Cool.

Click [HERE] to see a chart of decimal, hexadecimal and binary numbers.

[TOP][BACK]