Computers use base 2 (also called binary) for arithmetic. Binary numbers contain only 1 and 0 digits. The 1 and 0 digits are called bits. Each 1 bit represents a power of 2. It works like this:
1101 in binary means: 1 x 8 + 1 x 4 + 0 x 2 + 1 x 1 = 13 in decimal
People use base 10 (decimal) to write numbers. In base
10, all the place values are powers of 10.
But in binary, the place
values are all powers of 2. Compare the two systems:
Decimal : 3426 = 3 x 103 + 4 x 102 + 2 x 101 + 6 x 100
Binary : 1001 = 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20 = 8 + 1 = 9 decimal
This system is convenient for computers because they are built from transistors and circuits, which can be turned on or off, corresponding to 1 or 0. Since binary only uses 1 and 0 for bits, it is ideally suited to use in computers.
Another number system is base 16, named hexadecimal (hex for short). In hex there are more digits than in decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. So the digits A-F represent 10, 11, 12, 13, 14, 15. And in hex, the place values are powers of 16. The table below shows examples of numbers in one Byte (8 bits).
Hexadecimal |
Decimal |
Binary |
0 |
0 |
0000 0000 |
1 |
1 |
0000 0001 |
2 |
2 |
0000 0010 |
3 |
3 |
0000 0011 |
4 |
4 |
0000 0100 |
5 |
5 |
0000 0101 |
6 |
6 |
0000 0110 |
7 |
7 |
0000 0111 |
8 |
8 |
0000 1000 |
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 |
11 |
17 |
0001 0001 |
12 |
18 |
0001 0010 |
... |
... |
|
19 |
1x16 + 9 = 25 |
0001 1001 |
1A |
1x16 + 10 = 26 |
0001 1010 |
1B |
1x16 + 11 = 27 |
0001 1011 |
... |
|
|
1F |
16 + 15 = 31 |
0001 1111 |
20 |
2x16 + 0 = 32 |
0010 0000 |
21 |
2x16 + 1 = 33 |
0010 0001 |
... |
|
|
2E |
2x16 + 14 = 46 |
0010 1110 |
2F |
2x16 + 15 = 47 |
0010 1111 |
30 |
3x16 = 48 |
0011 0000 |
... |
... |
|
A1 |
10x16 + 1 = 161 |
1010 0001 |
BC |
11x16 + 12 = 188 |
1011 1100 |
FF |
15x16 + 15 = 255 |
1111 1111 |
Hexadecimal is annoying because of the calculations with 16 and the funny digits. But programmers like hex because hex takes a lot less space than binary, and it is very easy to convert between binary and hex. This means you can read the contents of a an entire file in hex much more easily than in binary, and it will probably make more sense in hex than in decimal.
Each nibble (half of a byte = 1 hex digit) consists of 4 bits in binary. That means you can convert hex numbers to binary one nibble at a time, without bothering to convert to base 10. You just need to memorize this table:
Hex |
Binary |
0 |
0000 |
1 |
0001 |
2 |
0010 |
3 |
0011 |
4 |
0100 |
5 |
0101 |
6 |
0110 |
7 |
0111 |
8 |
1000 |
9 |
1001 |
A |
1010 |
B |
1011 |
C |
1100 |
D |
1101 |
E |
1110 |
F |
1111 |
A hex editor displays all the bytes in a file in hexadecimal. This is useful when working on direct access files (also called binary files or RandomAccessFiles). Then you can see where data is stored and where empty spaces occur. You can also make changes to individual bytes - but this is usually a very bad idea as it can easily destroy the integrity of the data. For example, look at the following file dump of the Lockers.dat file:
The blue number 0E means 14 decimal. This byte tells the length of the UTF String following it. So the name "Greg V. Pillow" should contain exactly 14 characters, and indeed it does. However, looking at the red numbers starting in location 1D2B4, we see the byte 0B, indicating there should be 11 letters in the following name. However, there appear to be 14 letters. How can we explain this discrepancy? Programmers worry about such things when programming direct access files. Can you explain what "went wrong"?
(1) Run the Windows Calculator, and use it to fill in the blanks in the following:
1011 0101 bin = _______ dec = _______ hex
99 dec = ____________ bin = ______ hex
9F hex = ______ dec = _____________ bin
(2) Do the following conversions by hand (no calculator). Then use Windows Calculator to check:
0001 1111 bin = _____ dec = ______ hex
AA hex = _____ dec = _________________ bin
254 dec = _____ hex = ________________ bin (hint: What is 255 dec?
(3) Get a copy of the free hex editor XVI32 - you can download it at: http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm
Use it to "hack" the lockers.dat
file. Find a locker with a name in it (use your program to check),
then
change all the bytes in the name (including the length bytes up front)
to zeroes, to make the
locker empty.
Then run your program again and check whether that worked.
(4) Now use the hex-editor to write your name into the empty locker
that you made in (3) above.
Then use your program
to check whether you have "hacked" the file successfully.
(5) Copy this icon:
Then open the file using the hex-editor. In this BMP
file, there are 3 bytes representing the color of each pixel - one
byte for Red, one for Green, and one for Blue. White is a mixture of all
three primary colors. Figure out where the white pixels are, and change
some bytes to put some red and blue dots in the white area.
(6) The RWB.BMP icon is 24 pixels wide.
(a) Convert
24 dec to hexadecimal, without using a calculator.
(b)
Find this number in the icon file somewhere, and change that byte to FF
.
Save the file and
display it. Did it make the icon bigger?