Why should you care?
After learning binary, you'll quickly notice a problem.
Binary numbers become very long and difficult to read.
For example:
1111111111111111
Can you tell what number that is at a glance?
Probably not.
This is why programmers often use Hexadecimal (Hex). It is much shorter, easier to read, and maps perfectly to binary.
You'll encounter hexadecimal in many places:
- Memory addresses
- Colors in web development
- Debugging tools
- Machine code
- Network protocols
- Cryptography
- File formats
Understanding hexadecimal is an essential skill for every programmer.
The Problem
Computers store everything in binary.
Humans, however, struggle to read long binary numbers.
Consider this binary value:
1111000010101101
It is correct, but not very readable.
Now look at the same value in hexadecimal.
F0AD
Much shorter.
Both represent exactly the same data.
Hexadecimal exists to make binary easier for humans to work with.
The Concept
Hexadecimal is a Base 16 number system.
Instead of using ten symbols like decimal, it uses sixteen.
| Decimal | Hex |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| 10 | A |
| 11 | B |
| 12 | C |
| 13 | D |
| 14 | E |
| 15 | F |
After 9, hexadecimal continues with letters.
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
Simple Explanation
The reason hexadecimal is so useful is simple.
One hexadecimal digit represents exactly 4 binary bits.
| Binary | Hex |
|---|---|
| 0000 | 0 |
| 0001 | 1 |
| 0010 | 2 |
| 0011 | 3 |
| 0100 | 4 |
| 0101 | 5 |
| 0110 | 6 |
| 0111 | 7 |
| 1000 | 8 |
| 1001 | 9 |
| 1010 | A |
| 1011 | B |
| 1100 | C |
| 1101 | D |
| 1110 | E |
| 1111 | F |
Example:
Binary
1111 0000 1010 1101
Split into groups of four bits.
1111 0000 1010 1101
F 0 A D
Result:
1111000010101101₂ = F0AD₁₆
No complicated calculations are required.
Real-world Analogy
Imagine writing a phone number.
Which is easier to remember?
9876543210
Or:
9 876 543 210
Both contain the same information.
The second version is easier to read because it is grouped.
Hexadecimal does something similar for binary.
Instead of looking at sixteen individual bits, programmers look at four hexadecimal digits.
The information is identical, but the representation is much cleaner.
Code Example
Convert a decimal number to hexadecimal in Java.
public class Main {
public static void main(String[] args) {
int number = 255;
System.out.println(Integer.toHexString(number));
}
}
Output:
ff
Convert hexadecimal back to decimal.
public class Main {
public static void main(String[] args) {
String hex = "FF";
int decimal = Integer.parseInt(hex, 16);
System.out.println(decimal);
}
}
Output:
255
You can also display hexadecimal values with the common 0x prefix.
int value = 0xFF;
System.out.println(value);
Output:
255
Common Mistakes
Mistake 1
Thinking hexadecimal is another way of storing data.
It is not.
Computers still store everything in binary.
Hexadecimal is simply a human-friendly representation of binary.
Mistake 2
Treating A through F as characters.
In hexadecimal:
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
They are digits, not letters.
Mistake 3
Forgetting the base.
The number:
10
has different meanings depending on its base.
10₂ = 2
10₁₀ = 10
10₁₆ = 16
Always know which number system you are working with.
Mistake 4
Ignoring the 0x prefix.
Many programming languages write hexadecimal numbers like this:
0x1A
0xFF
0xABC
The prefix tells the compiler that the value is hexadecimal.
Advanced Notes
Memory Addresses
Most debuggers display memory addresses in hexadecimal.
Example:
0x7FFE9A34
Hexadecimal makes addresses much shorter than binary.
Web Colors
Every color on a web page is written in hexadecimal.
#FFFFFF
White
#000000
Black
#FF0000
Red
Each pair represents the intensity of:
- Red
- Green
- Blue
Bytes and Hex
One byte contains 8 bits.
Since one hexadecimal digit represents 4 bits:
8 Bits = 2 Hex Digits
Example:
11111111
↓
FF
This relationship makes hexadecimal ideal for representing bytes.
Debugging
When debugging applications, you will often see:
- Error codes
- Memory dumps
- CPU registers
- Machine instructions
These are usually displayed in hexadecimal because it is compact while still maintaining a direct relationship with binary.
Summary
Binary is the language computers understand.
Hexadecimal is the language programmers use to read binary more easily.
Decimal
255
↓
Binary
11111111
↓
Hexadecimal
FF
Remember these key facts:
- Hexadecimal is Base 16.
- It uses digits 0 to 9 and letters A to F.
- One hexadecimal digit equals four binary bits.
- Two hexadecimal digits equal one byte.
- Computers store binary, not hexadecimal.
Once you understand hexadecimal, topics like memory, networking, operating systems, and low-level programming become much easier to explore.
Suggested additions:
- Programs for converting between decimal, binary, and hexadecimal.
- Practice problems with solutions.
- A cheat sheet showing every 4-bit binary group and its hexadecimal equivalent.
- Diagrams explaining how bytes map to hexadecimal values.
Top comments (0)