Web info-x
   
THIS SITE IS FOR SALE
 
 
Tutorials
 Info-x : Info-x Tutorials and Documents : Tutorials
Message Icon Topic: Understanding the Hexadecimal system Post Reply Post New Topic
Author Message
deej
Admin Group
Admin Group


Joined: 22 Nov 1997
Online Status: Offline
Posts: 3
Quote deej Replybullet Topic: Understanding the Hexadecimal system
    Posted: 14 Dec 2004 at 15:45
Hex - what is it, and why do I need it?

By now, most of us are aware that computers are binary devices - that is,
everything in a computer comes down to just two states. Any written character,
any color, or any audio tone reproduced by a computer in any form exists as
a binary value. If this is news to you, take a few minutes to browse through
the binary number system
before continuing with this article, as a basic understanding of base-2 is important
if this article is going to make sense to you.

In our everyday world, we commonly use a numbering system universally known
as the decimal system or base-10. In this system, we use ten unique glyphs (numerals)
to represent known values. These values start at zero (represented by 0) and
increase to a maximum of nine (represented by 9). The greatest value that can
thus be expressed by a single glyph is the value \"nine\". In order to express
a value greater than nine, we must add a second glyph to our depiction of the
value. In this case, the placement of this new glyph is critical to our understanding
of the value represented by the glyph.

Consider the decimal value 75. When we see this value written as such, we
automatically understand that the glyph 7 in the left-most column represents
seven sets of ten, and that the glyph 5 in the right-most column represents
five sets of one. If the written value were to be a three-digit value, we would
then understand the now left-most column represents a certain quantity of a
differently valued set purely by virtue of its position in the written representation
of the total value.

We know all of this already, right? After all, we've been using this system
all of our lives. The decimal number system has the primary advantage of being
easily understood while not producing excessively clumsy written representations
of large values. We have even invented some shorthand to further simplify these
numbers. The terms million, billion, and trillion are proper terms used in the
decimal system, but don't we commonly refer to the value 32,780,000,000 as \"thirty
two point seven-eight trillion\"? That is more readily understood than the long
term \"thirty two trillion seven hundred eighty million\".

OK - back on topic - computers use the binary or base-2 system. Base-2 can
produce uncomfortably long written representations of even fairly low values.
Consider the decimal value 9. This value, which is represented by a single digit
in base-10, becomes 1001 in base-2 simplest form - a four-digit representation.
Base-2 was selected due to its direct correspondence to useful electrical states
(on or off) and basic decisions (yes or no). This works very well for extremely
low values, but becomes troublesome as the expressed value grows.

Let's look at the binary representation of the decimal value 365. In base-2,
this value would be written as 1 0110 1101. Do the arithmetic ... 2^8 + 2^6
+ 2^5 + 2^3 + 2^2 + 2^0 = 256 + 64 + 32 + 8 + 4 + 1 = 365. Again, if you don't
follow how this works, read the How-To that I referenced earlier. Let's face
it, as convenient as base-2 is for some things, it certainly is not so for expressing
large values. So if base-2 isn't good here, what would be? In order to maintain
some semblance of continuity, we must select a number system that allows simplified
expression of large values but that also has some commonality with the binary
system. This commonality is found in a number system whose base is also a power
of two. The decimal system (base-10) is out then, right? The most obvious choices
would be base-4, base-8, and base-16. Base-4 would not offer much advantage,
as it would still result in fairly lengthy expressions of large values. That
leaves us to choose between base-8 and base-16.

In reality, both base-8 (octal) and base-16 (hexadecimal) are used in the
computer field, although today, hexadecimal (abbreviated hex or simply h) is
in much more common usage. This number system is uses sixteen unique glyphs
as its digits, with the number of columns increasing as the expressed value
increases. Even the smallest values are customarily written in two-digit format
using a leading zero, and with a hex indicator following the expression: 04h
or 04hex. You will commonly see hex expressions used when discussing memory
addresses and Input/Output ranges as hardware resources.

The glyphs used, in increasing value, are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A,
B, C, D, E, and F. The table in Figure 1 illustrates the relationship between
decimal, binary, and hexadecimal expressions.


Decimal Binary Hex Decimal Binary Hex Decimal Binary Hex
0 0 00h 16 1 0000 10h 32 10 0000 20h
1 1 01h 17 1 0001 11h 33 10 0001 21h
2 10 02h 18 1 0010 12h 34 10 0010 22h
3 11 03h 19 1 0011 13h 35 10 0011 23h
4 100 04h 20 1 0100 14h 36 10 0100 24h
5 101 05h 21 1 0101 15h 37 10 0101 25h
6 110 06h 22 1 0110 16h 38 10 0110 26h
7 111 07h 23 1 0111 17h 39 10 0111 27h
8 1000 08h 24 1 1000 18h 40 10 1000 28h
9 1001 09h 25 1 1001 19h 41 10 1001 29h
10 1010 0Ah 26 1 1010 1Ah 42 10 1010 2Ah
11 1011 0Bh 27 1 1011 1Bh 43 10 1011 2Bh
12 1100 0Ch 28 1 1100 1Ch 44 10 1100 2Ch
13 1101 0Dh 29 1 1101 1Dh 45 10 1101 2Dh
14 1110 0Eh 30 1 1110 1Eh 46 10 1110 2Eh
15 1111 0Fh 31 1 1111 1Fh 47 10 1111 2Fh

Figure 1 - Decimal, binary, and hexadecimal expression comparison

Now it's time to talk about the part we all hate - the rules. Actually, they're
pretty simple in hex.

Always use an indicator when writing hex expressions. As I said earlier, the
most common are h or hex appended after the expression. Another less common
indicator is $, which is placed before the expression, as in $3E8. To reiterate,
$2F8, 02F8h, and 2F8hex are three different hex representations of the same
value. Indicators are extremely important if a hex expression contains only
numeric glyphs, as in 27h. This expression is equivalent a decimal value of
39. Can you imagine the confusion that would result if we omitted the indicator?
Always use upper-case letter glyphs within the value expression, but always
use lower-case letters in the indicator. Although it makes no real difference
to the reader, remember that an A is not the same as an a to the computer. When
writing a hex expression, bear in mind that we must break our life-long habit
of moving to the next column upon reaching 9 in the first column. In hex, our
first column tops out at F - if we want to increase the expressed value by one,
we then end up with 10h, which is equivalent to 16 decimal. I know that this
can be very confusing to the uninitiated. The first time I saw numeric expressions
with letters as digits, I was lost ... until I discovered the secret of hex
numbers. Now, I find it very sensible and logical, but I still think it looks
odd!

The columns in a hex expression are valued in powers of 16, with the right-most
column being 16^0, the next column to the left being 16^1, the third column
being 16^2, and so on to the extent of the expression. This being the case,
let's look at the expression 0378h. This value breaks down as follows:


0 x 16^3 = 0 x 4096 = 0
3 x 16^2 = 3 x 256 = 768
7 x 16^1 = 7 x 16 = 112
8 x 16^0 = 8 x 1 = 8



If we add all of these values, we arrive at a decimal value of 888, which is in
fact the decimal equivalent of 0378h. Got it yet? OK - let's look at something
practical now.

In a PC, COM1: is assigned to the base I/O address of 03F8h. The serial port
requires a range of eight I/O addresses in sequence. If that is the case, what
it the highest I/O address that COM1: will use? If we just count sequentially
through the eight hex addresses starting at 03F8h, we will end up at 03FFh ...
03F8, 03F9, 03FA, 03FB, 03FC, 03FD, 03FE, and 03FF. Where are these addresses
located in relation to the installed memory in the PC? They are right at the
top of the first kilobyte of physical RAM - 03F8h is 1016 decimal, and 03FFh
is 1023 decimal. Don't forget that we start numbering things at 0 in computers,
so that first kilobyte will be bytes 0 through 1023. See how it works?

Converting Hex to Decimal

There are two common methods of converting hex values to decimal values. One
of them has already been demonstrated. In this method, all that you need is
a powers of sixteen table and a calculator. This is the method that I prefer
if I must manually convert, as there is less to remember. This method simply
requires you to multiply the value in each column by the appropriate power of
16 for that column. Record each of the products as you determine it, and then
add them all together to get the decimal expression for the original hex value.

The next method requires you to first convert each digit to decimal. Start
with the left-most digit, and multiply its decimal value by sixteen. Record
the product as a subtotal and move on to next digit to the right. For each column
to which you advance (except the right-most column), you would first convert
that column's digit to its decimal equivalent, add that value to your running
subtotal, and then multiply the new subtotal by sixteen and record this value
as your newest subtotal. Keep on doing this - convert, add, and multiply until
you get to the last (right-most) column, where the routine changes. For this
column, once you have converted it to decimal and added it to your running subtotal,
stop - you're finished. Clear as mud, right? Let's try it ... let's convert
D809F3h to decimal. In the steps below, the running subtotal is shown in red,
and the values in parentheses are decimal equivalents for hex digits.


D (13) x 16 = 208
8 + 208 = 216 216 x 16 = 3456
0 + 3456 = 3456 3456 x 16 = 55296
9 + 55296 = 55305 55305 x 16 = 884880
F (15) + 884880 = 884895 884895 x 16 = 14158320
3 + 14158320 = 14158323



By using this method, we arrive at D809F3h being equivalent to 14158323 decimal.
This method works, but it requires that you remember the steps and follow them
faithfully. I find it confusing when compared to the method that I used earlier.
Let's apply that method to this hex value, to see if it gives us the same result
- which, obviously, it should. Using the first conversion method, we get the following:



D x 16^5 = 13 x 1048576 = 13631488
8 x 16^4 = 8 x 65536 = 524288
0 x 16^3 = 0 x 4096 = 0
9 x 16^2 = 9 x 256 = 2304
F x 16^1 = 15 x 16 = 240
3 x 16^0 = 3 x 1 = 3
_________
14158323



End result: both methods work. Now that we've got that out of the way - and that
we know how to manually convert hex values to decimal values, I'll let you in
on a secret. Remember I said earlier \"if I must manually convert...\"? That implies
that there are other ways, and so there are.

One of them is a hex-to-decimal convertor available through the web. Check
it out - it's pretty neat. This convertor performs direct conversion of any
three-digit hex value into its decimal equivalent, and works directly in your
browser, kind of like a mileage finder in a road atlas. If the hex value is
larger than three digits, there is an auxiliary table for converting four- and
five-digit values. We will talk more about another (and easier) conversion method
later on.

Converting Decimal to Hex

When it comes to manually converting a decimal value into its hexadecimal
equivalent, there is a method that is basically the reverse of one of our hex-to-decimal
conversion methods. Here's how to do it. Start by dividing the original decimal
value by sixteen, resulting in a quotient and a remainder. Set the quotient
aside, and then convert the remainder to hex - and remember, it will be less
than sixteen. This converted value becomes the right-most digit in the new hex
expression. If there is no remainder, the right-most hex digit will be a zero.
Now divide the quotient by sixteen and repeat the above process to arrive at
the next hex digit. This digit gets placed to the left of the first one. Keep
on doing this divide by sixteen and convert the remainder routine until you
arrive at a zero quotient. Whatever remainder you now have gets converted to
hex and becomes the left-most hex digit in the expression.

Let's try it out by converting 2135 decimal to its hex equivalent. The hex
digits arrived at in each step are shown in red.


2135 divided by 16 = 133, R7 7h
133 divided by 16 = 8, R5 57h
8 divided by 16 = 0, R8 857h



In this example, the remainders were all less than ten, so their decimal values
and their hex values were the same. As can be seen, this manual conversion method
works the way it should, and resulted in the calculated hex equivalent of 2135
decimal being 857h.

Problems and Solutions

Each of the conversion methods offered to this point has its drawbacks. One
obvious area for error is the universal use of a calculator in the conversions.
When doing so, the remainder in a division problem is shown as a decimal value
rather than a fractional value. In the above example, dividing 2135 by 16 results
in a displayed answer of 133.4375. OK - the 133 part is easy, but how about
the .4375 part? That value must itself be converted to its fraction-of-sixteen
form before then converting that to hex. See how errors can occur? The third
line of the example above gives us a displayed answer of 0.5. Again, the 0 part
is obvious, as the instructions tell us to look for a zero quotient. The .5
part can be easily misunderstood to be a remainder of 5 rather than the correct
remainder of 8.

The other conversion methods require the user to have web access, or to have
a table or chart available, or to follow complex and confusing mathematical
steps. Needless to say, all of these conditions are less than desirable. So
... is there a better way? You bet - I've saved the best for last.

Just about anyone who has a modern PC running a current version of Windows
has a built-in number system convertor that will instantly convert between decimal,
binary, octal, and hex, all at the click of a button. This amazing tool is the
Windows Calculator, and this capability is available when the calculator is
displayed in its Scientific Calculator mode.

Launch your calculator - Start / Run / CALC or Start / Programs / Accessories
/ Calculator will do the trick ... providing that the calculator applet was
installed with Windows. If not, don't worry - it can easily be added through
the Windows Control Panel. Once the calculator is running, click View / Scientific
to put the calculator into Scientific Mode.

To use this marvel, first verify that the radio button for the number type
you will input is selected. Type in the input value and click the radio button
for the desired output format. The display will now show that equivalent for
the original input value.

Note also that the calculator keypad will change to accurately reflect the
number system in use.

In Conclusion

It is highly unlikely that the ability to convert hex values will change your
life at all. On the other hand, it may just come in handy some day. The truth
of the matter is that this is just one of those esoteric topics that compu-geeks
are conversant with, but that few others worry about. If reading this helps
to increase your understanding of the modern PC and its systems, the article
will have served its purpose.

IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Bulletin Board Software by Web Wiz Forums version 8.04
Copyright ©2001-2006 Web Wiz Guide

This page was generated in 0.047 seconds.
  Log in  
User:
Pass:
Remember Me:
Register
Forgot Password
  Christmas Gifts  

Bar Gifts
Xmas Gifts for Him
Xmas Gifts for Dads
Gadgets and Gizmos
Sporting Gifts
Games
Unique Lifestyle Gifts
Geek Gifts
iPod Mains Charger More Gadgets

THIS SITE IS FOR SALE
Sedo - Buy and Sell Domain Names and Websites project info: info-x.co.uk Statistics for project info-x.co.uk etracker® web controlling instead of log file analysis