DEV Community

Shankar L
Shankar L

Posted on

Hexadecimal and Why Programmers Use It

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

It is correct, but not very readable.

Now look at the same value in hexadecimal.

F0AD
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Split into groups of four bits.

1111   0000   1010   1101
  F      0      A      D
Enter fullscreen mode Exit fullscreen mode

Result:

1111000010101101₂ = F0AD₁₆
Enter fullscreen mode Exit fullscreen mode

No complicated calculations are required.


Real-world Analogy

Imagine writing a phone number.

Which is easier to remember?

9876543210
Enter fullscreen mode Exit fullscreen mode

Or:

9 876 543 210
Enter fullscreen mode Exit fullscreen mode

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));
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

ff
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

255
Enter fullscreen mode Exit fullscreen mode

You can also display hexadecimal values with the common 0x prefix.

int value = 0xFF;

System.out.println(value);
Enter fullscreen mode Exit fullscreen mode

Output:

255
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

They are digits, not letters.


Mistake 3

Forgetting the base.

The number:

10
Enter fullscreen mode Exit fullscreen mode

has different meanings depending on its base.

10₂  = 2
10₁₀ = 10
10₁₆ = 16
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The prefix tells the compiler that the value is hexadecimal.


Advanced Notes

Memory Addresses

Most debuggers display memory addresses in hexadecimal.

Example:

0x7FFE9A34
Enter fullscreen mode Exit fullscreen mode

Hexadecimal makes addresses much shorter than binary.


Web Colors

Every color on a web page is written in hexadecimal.

#FFFFFF
Enter fullscreen mode Exit fullscreen mode

White

#000000
Enter fullscreen mode Exit fullscreen mode

Black

#FF0000
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Example:

11111111

↓

FF
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)