Book 1

Chapter 1 - What is Hex?


Defining Hex

Where does one start when attempting to define hex? It is not, by any stretch of the imagination, a simple thing, however it does have a standard definition. So, perhaps it would be best to start there:

Webster's Dictionary defines hexadecimal mathematically... Hecadecimal (Or "hex") Base 16. A number representation using the digits 0-9, with their usual meaning, plus the letters A-F (or a-f) to represent hexadecimal digits with values of (decimal) 10 to 15. The right-most digit counts ones, the next counts multiples of 16, then 16^2 = 256, etc.

So what the hell does all that jibbajabba mean, anyway? Let's go back to something that's probably a little more familiar to you - good ole decimal. Decimal is a numbering system, commonly known in the mathematical world as "Base 10." It is what people generally use to calculate values and amounts, i.e. "I have ten quarters in my pocket. That's enough for a single laundry load, washer and dryer!" Deci, the root of the word "decimal", means ten, and lo and behold, the decimal numbering system has ten digits in it - 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. When we wish to express a value larger than 9, we carry the extra into the "tens" column, as you may have heard it in an elementary math course.

Hexadecimal, similarly, has the root "hexa" in it. The Latin base "hex" means "six", and "a" in this case means add, so "hexa" means "add six." Therefore, hexa-decimal is 10+6, or 16! Hexadecimal numbering is in Base 16. Following our previous model, this numbering system has sixteen standard digits in it. This raises a problem, however! Our normal number set only has ten digits in it, so we must find stand-ins, if you will, for the other six. Thus, the hexadecimal numbering system is made up of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. After F, it follows similarly to decimal - 10, 11, 12, 13... 19, 1A, 1B, 1C, 1D, 1E, 1F, 20, 21, and so on and so forth.

Now, as you may know, computers have always thought in simple terms of "on" and "off" - indeed, the original computer consisted of several rooms of such on/off toggle switches. As the expanse of technology grew and, thus, the size of it shrank, these on/off switches could no longer be toggled manually (indeed, doing such was highly ineffective) and they came to be symbolized by zeroes and ones. A zero is an "off" switch, signalling that an electric current would not be passing through the switch, while a one is an "on."

The accuracy of the above information is probably subject to debate; the specifics, however, are not really important. What you do need to realize is that with any such boolean value (in other words, any such true/false, zero/one, either/or value), you're dealing with two possible outcomes. As such, computers think in multiples of two. For simplicity's sake, these boolean (or "binary") values are packed together in groups of eight. For definition's sake, a single one of these binary values (in other words, a zero or a one) is called a "bit", and each group of eight bits is called a "byte." A computer can calculate multiples of 2 much faster than it can do anything else, and as such we're stuck with these byte things even though it's much easier for humans, at least initially, to calculate multiples of ten.

As I mentioned previously, a byte is made up of eight bits. If each bit can have two possible values, then standard math will tell you that each byte can have 2*2*2*2*2*2*2*2, or 2^8, or 256 possible values. This is where hexadecimal comes in nice and handy. We're dealing with the values 0 through 255, and the hexadecimal value for 255 just happens to be FF, the last two-digit number before values start spilling into three digits!

So, now that we know why we're going to be editing base-16 data specifically, it's time to get some standard notation out of the way. $FF, FFh, and 0xFF mean exactly the same thing -- the hexadecimal number FF. Once we get into assembly programming, the $ in $FF is going to take on a special meaning, and some more notation is going to be learned. As such, I will be using the 0x prefix to denote hexadecimal numbers, so you won't have to unlearn old habits when the time comes.

Converting Hex

By and large, you will not need to know how to convert between decimal and hexadecimal for just about any project you wish to work on, as the computer or console thinks almost exclusively in hex anyway. However, when you get to script extraction, you -will- need to know how to obtain the decimal size of a range of values, and most hex editors will give you this value in hex. Sometimes you'll have a start and an end value, and you'll have to perform some simple arithmetic -- except in base 16 instead of base 10 -- to calculate the number of bytes between them.

To this end, I will introduce you to your first tool, which you don't even need to download -- you probably already have it installed on your computer. I am, of course, referring to the standard Windows calculator. Now, you're probably looking at that quaint little image to the right and wondering, "How does THAT thing help me with hex?" Well, we're going to be exploiting a little-known, little-used function of the Calculator.

News flash! The Windows calculator has a scientific mode that can not only do decimal, but also hexadecimal, octal (base 8), and binary! Go ahead and choose "Scientific" from the view menu (as demonstrated here.)

Converting between decimal and hexadecial with Windows' Calculator couldn't be more simple. All you need to do is type in the number you want in the appropriate mode -- here, the "Dec" radiobutton is filled, indicating that we are in decimal mode -- then activate the radiobutton of the appropriate mode. Try it out for yourself -- enter 31 in decimal, and switch over to hex mode. If you did it right, 1F should be displaying in the number pane.

You really don't need to worry about any of the other functions of Windows' Scientific Calculator; the only things that will be of interest here are adding and subtracting hex, converting between decimal and hexadecimal, and eventually, converting between binary and hexadecimal -- but that last one is a long way off.

Simple Hex Math

Hexadecimal arithmetic works in exactly the same way as standard decimal math. In hex, as in decimal, 1+2=3. However, as hex is in base 16, 9+1 does not equal 10. 9+1 is A, as hex counting tells us. 9+2=B, 9+3=C. Hex math is sort-of important, but I've never been very good at adding and subtracting much more than two or three past 9 manually. It takes practice, of course, and you'll get better at it with time. Here are some sample problems for you to try:

0x11+0x0A=
0x06+0x05=
0x02F9+0x10=
0x0388+0x24=
0x011413-0x0C13=

Try to do them in your head, if you can. Check here for your answers.