Hexadecimal

The first thing you need to know in order to even come close to hacking a rom, is how to use the hexadecimal number system. I must warn you though, this might seem a bit tricky at first, but please don't close this window and surrender until you have read through the entire page.

Also, I'd like to point out that I'm not all that familiar with english terms when it comes to mathematics. I'll try my best, but I can't promise that some words haven't been misspelled or simply used in the wrong context.

Now then, before we start, I'm going to assume that you:

  • Never have heard of any Hexadecimal system before
  • Doesn't know anything about the binary system either
  • Is a complete failure when it comes to mathematics
  • Have enough interest in romhacking to strain your brain a little during the next 10-20 minutes.

We'll start off with something we already know, namely the decimal number system. This system is what they taught us in the first grade, and consists of ten different numbers.

This system has the base "10", anything higher then "9" uses two or more numbers. In other words:

10, 100, 1000, 10000, 100000 and so on.

Those of you who know some about math, do know that there's a lot of things you can do with these numbers. But we will settle with using only three of the most basic calculations for now.

Addition
Subtraction
Multiplication Constants
(Not too much or for too long)

In case you're unsure of how those constants will work, this is an example where we multply "2" several times ("x" signifies that we're multiplying):

2 x 2 = 4 (simple huh?)

2 x 2 x 2 = 8 (2 x 2 = 4, 4 x 2 = 8)

2 x 2 x 2 x 2 x 2 x 2 = 64

Now, rather then typing all those 2:s (six of them to be precise), it'd be much simplier to add a constant of 6 after a single "2", thereby declaring that we are multiplying it six times. This is how it would look:

But since I don't have enough space to include this pic throughout this tutorial, I'll type it out like: 2^6 instead.

2^6 = 64

Before we move on, try to calculate 4^5. If you did it right, you should end up with 1024 (highlight).

But enough of that. The next step towards learning how the hexadecimal system works, is to first learn about the binary system. But don't worry, it'll be brief, and you don't need to grasp it completely before you move on to hex. But since it can be helpful, I'm going to include it anyway.

The Binary Number System

You've probably heard of this before, even though you may not be very skilled at binary calculations. It's a system used by computers (to take an example), and the system consists only of two numbers. "1" and "0".

This also means that the binary number system has "2" as base (the base is the amount of numbers in the system, the decimal system used 10 numbers, and had "10" as base).

  • A single binary number (i.e, a "1" or a "0") is called a bit.
  • 8 bits is called a byte.

Now then, how "much" can we write using only binary numbers? First of all, let's start with a single bit. The lowest possible number available to us in this case, is "0". The highest is "1", not very hard is it?

Let's head over to bytes then. A byte is 8 bits, and is therefore typed out as "xxxxxxxx" where "x" can be either "1" or "0". The highest binary number you can write using a byte (or 8 bits) is "11111111", while the lowest one is "00000000". A random byte might look something like "10010111" (just so that you get the picture, and to use as an example down below).

All of this isn't very helpful when it comes to learning romhacking (for a beginner that is), but before we start with the hexadecimal number system, let's go through how you can convert a binary number into a decimal one.

The formula is to take your number (in our case the example above, 10010111), and start from the number to your far right (10010111).

Multiply the number using the base (2) and the position as a constant. The position to the far right is "0", next one is "1".

When you're ready, add the results together and voila: You have your decimal number.

This can be bit hard to grasp at first, so here's a handy picture, showing the entire process. Comments are marked red.

There you have it. The decimal equivalent of "10010111" is 151. If you don't believe me, open up a calculator on your computer, switch to advanced mode, select binary and type 10010111. Then switch to decimal, and you'll see how 10010111 is converted to 151.

For the record, the highest number you can type with a singel byte is "11111111". The decimal number for this is 255, although the number of combinations you can make numbers to 256..

As you might have guessed by now, calculating binary numbers like this can be rather tiresome. For that reason, there's another system that makes converting binaries a lot easier. That system is the...

Hexadecimal Number System

Finally, this is the part that no romhacker could do without. As I mentioned, converting a binary number into a Hexadecimal is a lot easier then converting it into its decimal equivalent. But before we go inte that, here's an explanation of what hex actually is:

The Hexadecimal number system has 16 numbers altogether, and thus it has the base 16. However, it is still typed out using regular decimal numbers.

The problem is, that the decimal number system doesn't have 16 numbers, it only has 10. To compensate for that, the Hexdecimal system uses the first 6 letters in the alphabet as well (A-F). These are the Hexadecimal numbers, with their decimal equivalents beneath them:

Now, every Hexadecimal number is 1 byte in size (00000000 to 11111111) . A hexadecimal number is typed out using two hexadecimals (colored numbers above), each being 4 bits in size (xxxx-xxxx). The lowest possible value for one of these numbers is of course 0000, and the highest is 1111. "0000" in hexadecimal form thus is "0", while "1111" is "F".

  • 0 - F: Hexadecimal, four bits in size
  • 00-FF: Hexadecimal number, 1 byte in size

That means a hexadecimal numbers value ranges from 0000-0000 (00) to 1111-1111 (FF). In decimal form, 00000000 equals to 0, while 11111111 is 255.

Does that mean that a hexdecimal number can't be any higher then FF or lower then 00? Yes it does. Altogether, there are only 256 different combinations to form using the 16 numbers available to us. Chances are you'll see strings of hexadecimal numbers, for example "AF0B4588", but strings always consists of pairs, that can be separated (previous example should read "AF 0B 45 88").

Always take this into consideration. Many programs, including calculators, won't show numbers with "0" as value when they're positioned at the beginning of a number. A string that reads "A56F2" could also be typed as "0A56F2" (note that a "0" is present at the start).

Converting Hex

In order to convert hex into binary, you separate the hexadecimal pair and turn them both into decimal form. Then convert the separate decimals to binary and add them up to recieve your final binary number.

For example, take the Hexadecimal number "2C". Break it up into two separate numbers, and you'll have "2" and "C". Convert these into decimal, which gives you "2" and "12" (12 is decimal for "C"). Now convert 2 and 12 into binary form, and you're left with 0010 and 1100. Add these two together and the final result is 00101100.

That means that 2C = 00101100. Here's an image, showing the same procedure, exept with "4F" this time.

One important thing to remember (even though you may have learned it by now) is that this number system ends at "F", and not "9". It's sometimes easy to think of "80" as the next number in line starting with "79", but remember that we're always using 16 numbers. That means we have 7A, 7B, 7C, 7D, 7E and 7F before it's time to move on to 80.

Calculating with these extra numbers works in the same way as the decimal ones, exept that now we have 16 numbers to use.

To make it clear, here's an example with the same calculation written three times, once in hex, once with decimals and once in binary:

(Hex) F - A = 5
(Dec) 15 - 10 = 5
(Bin) 1111 - 1010 = 0101

(Hex) 35 + 06 = 3B
(Dec) 53 + 6 = 59
(Bin) 00110101 + 00000110 = 00111011

 

You may or may not understand any of this, but the imortant part is that you know just what a Hexadecimal number is. This way, you won't be confused when you open up your rom in an editor and see a bunch of numbers jumbled in with the letters A-F.

If you aspire to produce hacks of various games, this is something you might have to learn sooner or later. But if you're mainly interested in translations, the whole hex-to-decimal-to-binary concept isn't required unless you start digging into the more advanced apsects of romhacking. When translating in-game text, you usually work in hex only, without having to convert the numbers to binary or decimal.

And with that, the section about hex has come to an end. But in order to simplify things as much as possible, here's a complete table with every 1-byte-size hexadecimal combination there is.

Hexadecimal table

Go to Top

 

Copyright © 2007 Romhacks.net
Code, Layout and tutorials are Copyright © by Aeris130. Other material, such as programs, are copyright to their respective authors. We do NOT claim copyright regarding the actual information described in the tutorials, as the author didn't play any part in discovering them in the first place.