Skip to content
systemcoding.dev
Fundamentals

Hexadecimal Explained: Why Programmers Use Base 16

Learn what hexadecimal is, how each hex digit maps to four bits, and why base 16 is the standard shorthand for bytes, addresses and colours in C.

6 min read
Summarize in AI
Flat illustration of a hex dump with one byte highlighted, with the C, Python and Rust logos.

Open a debugger, a network capture, a memory map or a CSS file and you meet numbers like 0x7ffd4a2c, DEADBEEF and #1e90ff. Those are written in hexadecimal, base 16, and they are not an affectation. Hex exists because it is a compact way to write binary that a human can read, convert and compare without errors. This post explains the digits, the link to bits, how to read and write hex in C with GCC 14 or Clang 18, and where hex is the right choice and where it is not. It assumes you have read the post on binary numbers, or at least know that a bit is a 0 or a 1.

The sixteen digits

Decimal uses ten digits, 0 to 9. Hexadecimal needs sixteen, and since we have only ten numerals, it borrows the first six letters for the rest. The letters stand for the values ten to fifteen.

Hex digit01234567
Value01234567
Hex digit89ABCDEF
Value89101112131415

Case does not matter: ff, FF and Ff are the same number. Pick one style per project.

Each place in a hex number is worth sixteen times the place to its right. The number 2F is therefore 2 times 16 plus 15, which is 47. The number 1A3 is 1 times 256, plus 10 times 16, plus 3, which is 419.

Why base 16 and not another base

The reason is that sixteen is a power of two: 16 is 2 to the power 4. One hex digit therefore holds exactly four bits, and the mapping between hex and binary is a plain lookup with no arithmetic. Four bits are called a nibble, and each nibble is one hex digit.

BinaryHexBinaryHex
0000010008
0001110019
001021010A
001131011B
010041100C
010151101D
011061110E
011171111F

To turn a binary number into hex, group the bits in fours from the right and replace each group. The byte 10110100 splits into 1011 and 0100, which is B and 4, so the byte is B4. To go back, expand each digit into its four bits. Nothing like this works for decimal, because ten is not a power of two. The decimal number 180 tells you nothing about which bits are set without a calculation, while B4 tells you at a glance that the top nibble is 1011.

This is why hex dominates wherever bits matter. A byte is always exactly two hex digits, from 00 to FF. A 32-bit value is always eight digits, and a 64-bit value sixteen. The width is visible in the text.

Octal is the other power of two

Base 8 also has this property, at three bits per digit, and it was popular on machines whose word sizes were multiples of three. Hex won once the byte became the standard unit, since eight bits divide into two nibbles but not into whole groups of three. Octal survives in Unix file permissions, which a separate post covers.

Writing hex in C

C marks a hexadecimal constant with the prefix 0x or 0X, followed by digits. The compiler treats it as an ordinary integer, so 0xFF and 255 are the same value. The type of a hex constant depends on its size and its suffix, and an unsuffixed hex constant can take an unsigned type where the decimal equivalent would not, which is a subtlety the integer posts return to.

hex_literals.cc
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main(void)
{
    uint8_t  mask  = 0xF0;
    uint16_t port  = 0x1F90;
    uint32_t magic = 0xCAFEBABE;

    printf("mask  = 0x%02X (%u)\n", (unsigned)mask, (unsigned)mask);
    printf("port  = 0x%04X (%u)\n", (unsigned)port, (unsigned)port);
    printf("magic = 0x%08" PRIX32 " (%" PRIu32 ")\n", magic, magic);
    return 0;
}

The conversion %X prints an unsigned integer in upper-case hex, %x in lower case. The number between % and X, such as 02 or 08, is the minimum field width, and the leading 0 pads with zeros. The PRIX32 and PRIu32 macros from inttypes.h give the right length modifier for uint32_t on any platform, which is why they are preferred to guessing between %X and %lX. The mask and port values are cast to unsigned because small integer types are promoted to int in a variadic call, and %X expects unsigned int.

Text
mask  = 0xF0 (240)
port  = 0x1F90 (8080)
magic = 0xCAFEBABE (3405691582)

You can check one result by hand. 0x1F90 is 1 times 4096, plus 15 times 256, plus 9 times 16, plus 0, which is 4096 plus 3840 plus 144, and that sums to 8080. The port number 8080 is a classic example of a value that looks arbitrary in decimal and reads cleanly in hex as 1F90, where the two bytes 1F and 90 are the high and low halves.

Where hex is the right tool

Hex earns its place in these situations:

  • Memory addresses and pointers. Debuggers and printf("%p") print addresses in hex because alignment and page boundaries are obvious: a 4 KiB page boundary is an address ending in 000.

  • Byte-level data. Hex dumps, packet captures, file signatures and checksums show each byte as two digits.

  • Bit masks and register values. 0x80000000 shows that only the top bit is set, which 2147483648 hides.

  • Colours. An RGB colour such as #1e90ff is three bytes, one per channel: 1e red, 90 green, ff blue.

  • Character codes and Unicode. U+00E9 names the code point for the letter e with an acute accent.

Where hex is the wrong tool

Hex is for bit patterns, not for quantities. A loop count, a price or a number of items belongs in decimal, because readers reason about those values by magnitude and not by bits. Writing for (i = 0; i < 0x64; i++) makes the reader convert the bound for no gain. A good rule: if you would ever ask which bits are set, use hex, and if you would ask how many, use decimal.

Converting by hand, briefly

To convert hex to decimal, multiply each digit by its place weight, which is a power of sixteen, and add: 0x3E8 is 3 times 256 plus 14 times 16 plus 8, which is 768 plus 224 plus 8, giving 1000. To convert decimal to hex, divide by 16 repeatedly and read the remainders from last to first, writing 10 to 15 as A to F. Dividing 1000 by 16 gives 62 remainder 8, then 62 by 16 gives 3 remainder 14, which is E, then 3 by 16 gives 0 remainder 3, so the digits from last to first are 3E8. The full walkthrough, including a binary shortcut, is in the post on converting by hand.

Common mistakes

  • Forgetting that 0x is a prefix and not part of the value. In a data file or a protocol, FF and 0xFF are written differently, and a parser needs to know which one to expect.

  • Writing a hex constant that does not fit the target type, such as uint8_t x = 0x1FF;. The value is converted to the type and the high bits are lost, which GCC reports with -Woverflow for constants.

  • Mixing up the 0x integer prefix with the hex character escape. In C source, a backslash followed by x41 inside a string or character constant is the character with code 65, while 0x41 is an integer constant with the same value.

  • Reading 0x10 as ten. It is sixteen.

Taggedcbeginner
Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.