|Written by Harry Fairhead|
|Tuesday, 19 November 2013|
Page 1 of 2
Hexadecimal is the most common way of displaying the raw data sitting in a machine's memory, but if you are not familiar with it you might ask "What the hex..?"
What the hex?
Hexadecimal is the most common way of displaying the raw data sitting in a machine's memory or even stored on disk. You can be happily programming away in a high level language without a care in the world and then suddenly an serious error occurs and you are faced with a line showing you the address of the problem and the contents of the processors registers etc. all in glorious hex.
Even if you are not programming, the most usual format to dump a file in is as lines and lines of hex. Back in the dark old days of assembly language programming you had to be familiar with hex as well as with binary and occasionally octal.
If you haven't mucked about with assembler or machine architecture, or if you fell asleep in the first term of the computer course, then you might think hex is just a programmer's curse. To make sure that you know better this is a short, and highly practical guide, to hexadecimal addressing and data. If you are no good at math don't panic because it is very simple and after a few minutes practice it becomes almost second nature.
If only we had all been born with 16 fingers!
Not only would typing have been a faster activity but we might have counted in hex naturally.
Counting is a matter of using symbols to represent each number. For example, 0, 1, 2, 3 and so on. Using this simple system the problem is that you quickly run out of symbols. You need a symbol for each possible quantity of things.
The solution is to use a place value counting method.
In decimal we are all very happy with this method - you count up to 9 and then start again after recording the fact that you have got to 10 once by writing a 1 to the left.
In school we are taught that each place to the left represents 10 times more than the previous digit location. That is, the first value represents units, the second lots of 10, the third lots of 100 and so on. This means that a number like 123 is really:
or one lot of 100, two lots of 10 and three lots of 1.
It is generally supposed, and I can think of no better explanation, that we count in lots of 10 because that's how many fingers we have.
All of this is so easy that we tend to use it intuitively and without being able to explain what it going on. This makes the shock of changing to a different counting base even more traumatic.
Hexadecimal uses 16 as the base - Hexa=6 and decimal=10.
To put this another way the hex counting system uses `lots of 16' in the same way that the decimal system uses `lots of 10'.
The first problem to be solved is how to count up to the first lot of 16 as there are only ten digits - 0 to 9. The solution is that we use the letters A, B, C, D, E and F to supplement the meager inheritance that having only ten fingers has bestowed upon us.
This means that counting in hex up to 15 goes
The only tricky bit being to remember not to say `ten' after 9.
Now what happens after counting to F?
This is, the same question as what happens after counting to 9 in decimal.
The answer is that you write a 1 to the left to indicate that you have counted one lot of 16 and then carry on counting. That is after F comes 10 which isn't ten (decimal) and shouldn't really be said as ten but `one nought' or `one zero'.
The next question is what comes after 10 in hex?
The answer is 11, then 12 and so on all the way up to 1F.
At this point we have another lot of 16 and we start counting again at 20 and so on. The only danger point happens when you reach 9F and the temptation is to accidentally make the next value 100. It isn't because in hex A comes after 9 and so the next value is A0 and so on.
You only reach 100 in hex after counting all of the way to FF.
Fun isn't it?
Once you have learned to count to 100 hex there isn't anything more to see. You just keep counting up to F and adding one to the place to the left until it reaches F and so on.
Many programming languages use the convention, first used in C, that a hex number is indicated by starting with 0x. So if you have counted to FF you can write this as 0xFF.
Remember: the hex place value system works with lots of 16 and not lots of 10.
Being able to count in hex is a great way to understand the how the system works but it isn't much use when you are confronted with a message such as
To really feel at home with hex you have to be able to understand it in a slightly different way. You certainly have to be able to convert hex to decimal and vice versa but there is something deeper.
First, though, how do you convert hex to decimal?
There are a number of standard algorithms that can be used to convert between different number bases but I have to admit that I prefer a more primitive approach. The place values used in hex are:
and you can use these to work out the equivalent decimal value quite easily.
For example, AD45 is simply:
to express it in a mix of hex and decimal, or moving entirely to decimal:
which works out to:
To convert from decimal to hex is just a little more complicated in that you have to discover how many lots of 4096, 256 and 16 there are in a number. For example, 44357 contains 10 lots of 4096 because:
The remainder, i.e.
is 3397 and this contains 13 lots of 256 because:
and so on to discover that the remainder contains 4 lots of 16 and 5 units. Writing 10 lots of 4096, 13 lots of 256, 4 lots of 16 and 5 units in standard hex gives AD45.
Fortunately it isn't often that you have to convert decimal to hex!
|Last Updated ( Tuesday, 19 November 2013 )|