Hexadecimal — Four Bits per Digit
Look at a colour code like #FF8800, a memory address like 0x7ffe, or a network card's address like 3c:22:fb:10:9a:01. They are all hexadecimal. Programmers did not pick base sixteen to be difficult. They picked it because it lines up perfectly with bits.
The rule
One hex digit is four bits; two hex digits are one byte.
Hexadecimal (hex) is base 16. It needs sixteen digits, so after 0–9 it borrows letters:
hex: 0 1 2 3 4 5 6 7 8 9 A B C D E F
decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
binary: 0000 … 1001 for 0–9, then 1010 1011 1100 1101 1110 1111Four bits have exactly 16 patterns, so each hex digit stands for one group of four bits (sometimes called a nibble). A byte is two nibbles, so it is always exactly two hex digits, from 00 to FF. A 32-bit number is always 8 hex digits.
Hex is usually marked with a 0x prefix, as in 0x2F, so it is not confused with decimal.
For the decimal value, the places are worth 1, 16, 256, … So 0x2F = 2 × 16 + 15 = 47.
The shortcut
Never convert hex to binary through decimal. Translate each hex digit into its own four bits, and glue the groups together. Going the other way, split the bits into fours from the right and translate each group.
Split into digits
0x3A has two digits: 3 and A.
The trap: treating 0x10 as ten. In hex, 10 means one sixteen and no ones, which is decimal 16.
Drills
Answer fast — trust the rule.
What is 0x1A in decimal?
What is the byte 1100 0011 in hex?
How many hex digits does it take to write any 32-bit number?
What is 0xFF in decimal?
In the colour #FF8800, how much of each colour channel is there?
Remember
- One hex digit is four bits; two hex digits are one byte.
- Translate digit by digit between hex and binary; never go through decimal.
- The trap: 0x10 is sixteen, not ten — always check the prefix.
