Skip to main content
Be Bitwise

Hexadecimal

Binary & Number Systems · Foundations track

4 min read

Part ofSystems Programming Companion, week 1

In this module

Why Hexadecimal?

Binary is how computers think. Humans find it hard going. A single byte like 11010110 is already a slog at a glance, and real-world values run dozens of bits long. We need a compact way to write binary without severing the link to the underlying bits.

Enter hexadecimal (hex, to its friends). Hex is a base-16 number system that maps cleanly onto binary, which is why it's the preferred shorthand all over low-level programming.

The Hex Digits

Decimal uses 10 digits (0-9). Hex needs 16, so it pinches the first six letters of the alphabet:

DecimalHexBinary
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
10A1010
11B1011
12C1100
13D1101
14E1110
15F1111
Key Concept

Each hex digit represents exactly 4 bits. This means one byte (8 bits) is always written as exactly two hex digits. The binary value 11010110 becomes D6 in hex - split it into 1101 (D) and 0110 (6).

Converting Between Binary and Hex

The conversion is purely mechanical:

  1. Binary to hex: Split the binary number into groups of 4 bits, starting from the right. Look up each group in the table above.

    • 10111110 becomes 1011 | 1110 which is BE.
  2. Hex to binary: Replace each hex digit with its 4-bit binary equivalent.

    • 3F becomes 0011 1111.

No multiplication, no division. Just pattern matching.

A few byte values turn up so often that they're worth knowing on sight. Before you press each button below, say the eight bits out loud, one nibble at a time, then check. Afterwards, click bits to build BE and 3F from the examples above.

27
26
25
24
23
22
21
20
Decimal0
Hex0x00
Binary00000000
FieldBitsHexDecimal
high nibble7-40x00
low nibble3-00x00

Worked Example: Hex and Decimal

Going between hex and decimal takes a little arithmetic, because 16 isn't a power of 10. Each hex position is worth 16 times the one to its right: 1, 16, 256, 4,096 and so on.

Hex to decimal. For 0x2F, multiply each digit by its place value: 2 × 16 + 15 × 1 = 47. For 0x1A3: 1 × 256 + 10 × 16 + 3 × 1 = 419.

Decimal to hex. Divide by 16 and keep the remainders. For 200: 200 ÷ 16 is 12 remainder 8. The quotient 12 is C and the remainder is 8, so 200 is 0xC8. Bigger numbers just need more rounds of dividing, reading the remainders from last to first.

You'll rarely do this by hand for long, but being able to sanity-check a value in your head is worth the effort. When a debugger says a length is 0x400, it's handy to know at a glance that's 1,024.

Reading Hex in a Memory Dump

There's one wrinkle that trips up nearly everyone the first time. x86 and most ARM systems store multi-byte numbers little-endian: least significant byte first, at the lowest address. Store the 32-bit value 0xDEADBEEF and a memory dump shows its bytes the other way round:

text
address: 1000 1001 1002 1003
bytes: ef be ad de

To read a number out of a dump, take its bytes and reverse them. The hex digits inside each byte never get swapped, only the order of the bytes. So ef be ad de is 0xDEADBEEF, and ff 00 00 00 is 0x000000FF, which is 255.

Predict: an x86 memory dump shows the four bytes 2c 5a ff 7f. What 32-bit value is stored there?

0x7FFF5A2C. Reverse the byte order (7f ff 5a 2c) and join them up. That's the address from the list below. Debuggers often do this reversal for you when you ask for a whole word, but raw byte dumps and network captures won't.

Now try it on a real dump. Each question asks for a 32-bit value, so click the first of its four bytes, reverse them in your head and type the answer as decimal or as 0x hex. Switch to Explore if you want to see every way those bytes can be read.

Little-endian puzzle

Three 32-bit numbers stored by an x86 machine, then four ASCII characters.

Select

Click a byte to select 4 bytes starting there.

The ASCII column shows printable characters and a dot for every other byte.

Question 1 of 3

What 32-bit little-endian value starts at offset 0x00?

Where You Will See Hex

Hexadecimal turns up everywhere in computing:

  • Memory addresses like 0x7FFF5A2C. The 0x prefix tells you the number is in hex.
  • Colours in web development like #FF0000. That's 255 red, 0 green, 0 blue.
  • Error codes and debugging output. Debuggers print byte values in hex by default.
  • MAC addresses like AA:BB:CC:DD:EE:FF.

Try It Yourself

The converter below takes a number in any base and shows its representation in the others. Try entering 255 in decimal and watch what happens in binary and hex.

00101010
Range: 0 – 255 (8-bit unsigned)

Once you can move between binary, decimal, and hex without breaking stride, you have the vocabulary to read almost any low-level output you'll encounter.