Hexadecimal
Binary & Number Systems · Foundations track
4 min read
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:
| Decimal | Hex | Binary |
|---|---|---|
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 2 | 2 | 0010 |
| 3 | 3 | 0011 |
| 4 | 4 | 0100 |
| 5 | 5 | 0101 |
| 6 | 6 | 0110 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| 9 | 9 | 1001 |
| 10 | A | 1010 |
| 11 | B | 1011 |
| 12 | C | 1100 |
| 13 | D | 1101 |
| 14 | E | 1110 |
| 15 | F | 1111 |
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:
-
Binary to hex: Split the binary number into groups of 4 bits, starting from the right. Look up each group in the table above.
10111110becomes1011 | 1110which isBE.
-
Hex to binary: Replace each hex digit with its 4-bit binary equivalent.
3Fbecomes0011 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.
| Field | Bits | Hex | Decimal |
|---|---|---|---|
| high nibble | 7-4 | 0x0 | 0 |
| low nibble | 3-0 | 0x0 | 0 |
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:
address: 1000 1001 1002 1003bytes: ef be ad deTo 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.
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. The0xprefix 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.
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.