4.11. Hexadecimal¶
Hexadecimal is base 16. Much like base 10 uses digits that represent powers of 10 and binary uses digits that represent powers of 2, hexadecimal uses digits that represent powers of 16. Here is how we would interpret
4096s |
256s |
16s |
1s |
---|---|---|---|
2 |
0 |
4 |
7 |
So
Using this scheme,
Decimal |
Hexadecimal |
---|---|
10 |
A |
11 |
B |
12 |
C |
13 |
D |
14 |
E |
15 |
F |
Thus in the hexadecimal number
256s |
16s |
1s |
---|---|---|
1 |
14 (E) |
10 (A) |
So
Conversion To and From Binary
In hexadecimal, each digit can represent 16 different values: 0-F (0-15). In binary, we can represent the same number of values using 4 bits: 0000-1111 (0-15). What that means, is that each hex digit represents the same information as four binary digits - there is a direct mapping between a hex digit and a four-bit pattern:
Binary |
Hexadecimal |
Binary |
Hexadecimal |
|
---|---|---|---|---|
0000 |
0 |
1000 |
8 |
|
0001 |
1 |
1001 |
9 |
|
0010 |
2 |
1010 |
A |
|
0011 |
3 |
1011 |
B |
|
0100 |
4 |
1100 |
C |
|
0101 |
5 |
1101 |
D |
|
0110 |
6 |
1110 |
E |
|
0111 |
7 |
1111 |
F |
To convert binary to hex, simply break up the number into groups of 4 digits and convert each group:
011011000011 (1731 in decimal) 011011000011 6 C 3 6C3 (1731 in decimal)
To convert hex to binary, we can just turn each hex digit into one group of four binary digits:
A1 (161 in decimal) A 1 10100001 10100001 (161 in decimal)
This video reviews how hexadecimal works and provides a few more examples:
Why Hexadecimal
Why would we want another base system? Large binary values are hard for people to read accurately and remember. Try it yourself - quickly try to decide if 0011100101110110 and 0011100101100110 are the same. Those two binary strings listed above look like this in hex: