Table of Contents
Understanding Hexadecimal
Hexadecimal, often shortened to “hex,” is a way to write numbers using base 16 instead of base 10. In everyday life you use base 10, which has ten digits from 0 to 9. Hexadecimal has sixteen digits. This makes hex very useful in computing, especially when you work with binary, because it gives a compact and readable way to represent long binary patterns.
In this chapter you will see what the hex digits are, how to read and write hex numbers, and why hex is convenient when dealing with networking and low level data. You will connect it later to binary conversions and data representation, so the goal here is to understand the system itself and how to use it in simple conversions.
Hexadecimal Digits
Hexadecimal needs sixteen symbols. The first ten are the same as decimal. For the extra six values, the letters A to F are used.
| Hex digit | Decimal value |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| A | 10 |
| B | 11 |
| C | 12 |
| D | 13 |
| E | 14 |
| F | 15 |
A single hexadecimal digit can therefore represent any value from 0 to 15. When you see A in hex, you should think “ten.” When you see F, you should think “fifteen.”
Place Values in Hex
Hexadecimal is a positional number system. This means the position of each digit matters, just like in decimal.
In decimal, from right to left, the places are powers of 10:
- Ones: $10^0 = 1$
- Tens: $10^1 = 10$
- Hundreds: $10^2 = 100$ and so on.
In hexadecimal, from right to left, the places are powers of 16:
- Ones: $16^0 = 1$
- Sixteens: $16^1 = 16$
- Two hundred fifty six: $16^2 = 256$
- Four thousand ninety six: $16^3 = 4096$ and so on.
This pattern leads to a general rule for the value of a hex number.
Rule: Value of a hexadecimal number
For a hex number with digits $d_{n-1} d_{n-2} \dots d_1 d_0$ (each $d$ is 0 to 15), its decimal value is
$$\text{value} = \sum_{i=0}^{n-1} d_i \cdot 16^i$$
The rightmost digit has $i = 0$.
For example, take the hex number 2A. Here:
- A is the ones place and means 10.
- 2 is the sixteens place and means $2 \times 16 = 32$.
So the total decimal value is $32 + 10 = 42$.
Writing and Reading Hex Numbers
In many technical texts hexadecimal numbers are marked clearly so that you do not confuse them with decimal. There are several common notations:
| Notation style | Example | Meaning |
|---|---|---|
| Suffix h | 2Ah | Hex 2A |
| Prefix 0x | 0x2A | Hex 2A |
| Prefix 0X | 0X2A | Hex 2A |
You may also see hex numbers written without any prefix or suffix when the context is clear, for example inside an IPv6 address or a MAC address. In this chapter you can assume that any value that contains letters A to F is hexadecimal.
When you read hex numbers, it helps to translate each digit to its decimal value in your head, then remember which place value it has. For a beginner it is useful to say it aloud. For example, 3F in hex can be read as “three times sixteen plus fifteen.”
Manual Conversion from Hex to Decimal
You will have a separate chapter that covers number conversions in general. Here you will see the specific method for hex to decimal, because it is central to understanding hex itself.
To convert a hexadecimal number to decimal, follow these steps:
- Write down the hex number and label the positions, starting from 0 at the right.
- Convert each hex digit to its decimal value.
- Multiply each value by $16^{\text{position}}$.
- Add the results.
For example, convert 0x1C to decimal.
- C is 12 and is at position 0, so you have $12 \times 16^0 = 12 \times 1 = 12$.
- 1 is 1 and is at position 1, so you have $1 \times 16^1 = 1 \times 16 = 16$.
- Add them to get $16 + 12 = 28$.
You can set this up in a small table to keep track:
| Hex digit | Position | Decimal value | Power of 16 | Contribution |
|---|---|---|---|---|
| 1 | 1 | 1 | $16^1 = 16$ | $1 \times 16=16$ |
| C | 0 | 12 | $16^0 = 1$ | $12 \times 1=12$ |
Total decimal value is $16 + 12 = 28$.
Here is another example with three digits, 0x2F4.
- Positions from right to left are 0, 1, 2.
- 4 is 4 at position 0: $4 \times 16^0 = 4 \times 1 = 4$.
- F is 15 at position 1: $15 \times 16^1 = 15 \times 16 = 240$.
- 2 is 2 at position 2: $2 \times 16^2 = 2 \times 256 = 512$.
- Total is $512 + 240 + 4 = 756$.
Once you know the first few powers of 16, this process becomes much faster.
Powers of 16 to Remember
You do not need to memorize many powers of 16 to work comfortably with hex. The most useful ones fit into a small table.
| Exponent | Power of 16 | Decimal value |
|---|---|---|
| $16^0$ | 1 | 1 |
| $16^1$ | 16 | 16 |
| $16^2$ | 16 × 16 | 256 |
| $16^3$ | 16 × 16 × 16 | 4096 |
| $16^4$ | 16⁴ | 65536 |
With this table, you can quickly work out many common hex values. For example, hex 100 is
- $1 \times 16^2 = 1 \times 256 = 256$,
because the two zeros add no extra value.
Ranges Representable with Hex Digits
Because each hex digit can hold 16 values, you can easily compute the range of values that can be stored with a certain number of hex digits.
For one hex digit you have values from 0 to 15.
For two hex digits you have $16^2 = 256$ possible combinations, from 00 to FF.
For three hex digits you have $16^3 = 4096$ combinations, from 000 to FFF.
The maximum value with $n$ hex digits is always one less than $16^n$.
Rule: Maximum value for $n$ hex digits
With $n$ hexadecimal digits, the maximum integer value is
$$\text{max} = 16^n - 1$$
For example, with 2 digits the maximum is $16^2 - 1 = 256 - 1 = 255$, which is FF in hex.
This rule shows up a lot in networking, for example when you look at limits on fields inside protocol headers.
Grouping and Formatting Hex Values
In practice, long hex sequences are split into groups to make them easier to read. You will see several grouping styles in networking:
- Bytes written as pairs of hex digits, for example 4A, FF, 09.
- Bytes separated by colons or hyphens, for example 01:23:45:67:89:AB or 01-23-45-67-89-AB.
- Groups of four hex digits separated by colons, as in many IPv6 addresses.
Each pair of hex digits forms a natural unit, which you will later learn to relate to a byte. For now the key idea is that 2 hex digits are usually read together as one chunk of information.
When you see many hex digits next to each other, try to break them into groups of two from the right. This will help you read and work with them more easily.
Where Hexadecimal Appears in Networking
Hexadecimal is common throughout networking because it makes long binary values shorter and more readable, while still mapping cleanly to the underlying bits. You will see hex in several places later in the course, such as:
- Addresses that contain letters and numbers, for example specific address formats that do not look like decimal IPv4 addresses.
- Representations of protocol headers in packet analysis tools.
- Configuration fields that are given as hex values in device interfaces or logs.
You do not need to understand those networking uses yet. At this stage it is enough to recognize that when you see values with A to F in them, you are most likely looking at hexadecimal, and to be comfortable reading and converting those numbers.
Practicing with Simple Hex Values
To get used to hexadecimal, it is useful to become fluent with small hex numbers, especially the range from 0 to FF. These are the values that appear most often when data and addresses are shown in hex form.
Here are some simple examples that you can practice in both directions in your head:
| Hex | Decimal |
|---|---|
| 0 | 0 |
| 9 | 9 |
| A | 10 |
| F | 15 |
| 10 | 16 |
| 1F | 31 |
| 20 | 32 |
| 2A | 42 |
| 3C | 60 |
| 7F | 127 |
| FF | 255 |
If you can look at values like 7F and FF and quickly recall their decimal meanings, later parts of networking, such as reading address fields and packet dumps, will feel much more natural.
In later chapters you will connect hexadecimal directly to binary and learn faster methods for converting between the two. For now, a clear understanding of what hex is, how place values work, and how to perform basic hex to decimal conversion is enough.