The hexadecimal and binary numbers system

Thanks to Nemesis Sonic 2 Hacking Guide.

The binary numbers system

Explanation of a bit of data

First of all, you have to know that every file, every folder, everything in a computer is stored with a 0 or a 1 value. A computer can´t understand properly decimal numbers. Then, all data is stored with the help of 0´s and 1´s, corresponding to an electrical statement (1 is a high state, 0 is a null state). This is called a binary form, and a 0 or a 1 is called a bit (unit of memory).

How to count with positive binary numbers

In binary you count in twos instead of tens.
For a computer, the decimal number 2 as you can see it on your screen is stored on the hard drive with a binary form, the binary number 10. For every decimal number, there is a binary form corresponding:

Binary Decimal
0 0
1 1
10 2
11 3
100 4
101 5
110 6
111 7
... ...

You can notice that count in binary is not so difficult. Simply take every number with 0´s and 1´s only, and place them in ascendant order:
0 < 1 < 10 < 11 < 100 < 101 < 110 < 111 < 1000

The negative numbers

Another thing to know is there is not really negative values in binary. A negative value in decimal is represented by a binary number, the last we can count in our context. Then, if our numbers are between -127 and 127:

Binary Decimal
1000 0000 -127
1000 0001 -126
... ...
1111 1111 -1
0000 0000 0
0000 0001 1
... ...
0111 1110 126
0111 1111 127
1000 0000 -127
... ...



The hexadecimal numbers system

I know, for newbies, this is a bit boring and hard to understand. This is also a bit difficult to explain it properly...
If you understood the binary numbers system very well, then this part will be very easy for you.

What is an hexadecimal number?

In the hexadecimal system, numbers don´t stop at 9 and then 10. Instead, between 9 and 10, you will add 6 other numbers represented by a letter: A B C D E F. That&acutes why it is called hexadecimal, you count in sixteens instead of tens. There is a table with some values:

Hexadecimal Decimal Binary
0 0 0000 0000
1 1 0000 0001
... ... ...
9 9 0000 1001
A 10 0000 1010
B 11 0000 1011
C 12 0000 1100
D 13 0000 1101
E 14 0000 1110
F 15 0000 1111
10 16 0001 0000

Explanation of a nyble of data

Notice the similarities between hexadecimal and binary. When there is only one number in hex, we need only 4 bits to represent it in binary. These 4 bits are called a nyble.
Ex: 1 2 5 9 B 1011 and F are all a nyble of data.

How to convert hex values into bin values easily

To convert from hex to bin and from bin to hex, this is now very easy. When you have two numbers for an hex value, like 9C for example, you need two nybles (8 bits) to convert this number into bin.
An example is more accurate than a long and theorical explanation here:
9 in hex is 1001 in bin.
C in hex is 1100 in bin.
Then, 9C in hex is 1001 1100 in bin.

The best way is to learn perfectly each 16 bin values corresponding to each 16 hex values from 0 to F.

But what is the interest to count in hex rather than in bin?
When you edit a file under an hex editor, this is more easy to understand hex values, smaller than the bin values.

Bits, nybles, bytes and others...

I will explain here what does that mean. We consider this string of data: 2B 15 F6 4A

2B 15 F6 4A
0010 1011 0001 0101 1111 0110 0100 1010

Now, with some others colors:

2B 15 F6 4A
0010 1011 0001 0101 1111 0110 0100 1010

The orange part is a bit.
The blue part is a nyble (four bits).
The green part is a byte (eight bits, also known as an octet).
The red part is a word (two bytes).
All this string (four bytes) is called a long.
1024 bytes is a kilobyte (Kb or Ko).
1024 kilobytes is a megabyte (Mb or Mo).
1024 megabytes is a gigabyte (Gb or Go).
1024 gigabytes is a terabyte (Tb or To, very unusual).

That is the end of this part!


Back to: Tutorial about hexadecimal
Table of contents