Hexadecimal for whole numbers
Hexadecimal and Whole Numbers
Hexadecimal is a base 16 number system. While binary uses only 0 and 1, and decimal uses digits 0-9, hexadecimal uses sixteen symbols: the digits 0 to 9 and the letters A to F. The letters represent values that have no single digit in decimal: A = 10, B = 11, C = 12, D = 13, E = 14, and F = 15.
| 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 |
Like all positional number systems, each digit position in hexadecimal has a place value based on a power of 16. For two-digit hex numbers (the range needed for 0-255), there are two columns:
| 161 | 160 |
|---|---|
| 16 | 1 |
To find the decimal value: multiply the left digit by 16, multiply the right digit by 1, and add the results together. Two hex digits can represent values from 00 (0) to FF (15×16 + 15×1 = 240+15 = 255) - exactly the range of one byte, which is why two hex digits represent one byte.
Converting Hexadecimal to Decimal
The three tabs below work through conversions from the simplest case to the full complexity of two-digit hex numbers using letter digits.
Single Digit - Units Column Only
Convert 0E to decimal.
| 16 | 1 |
|---|---|
| 0 | E |
E = 14. Left digit is 0, so it contributes nothing. Right digit: 14 × 1 = 14.
When the left digit is 0, only the right digit matters. This is equivalent to any single-digit hex value (0 to F, which is 0 to 15 in decimal).
Two Digit - Number Digits Only
Convert 37 to decimal.
| 16 | 1 |
|---|---|
| 3 | 7 |
Left digit: 3 × 16 = 48. Right digit: 7 × 1 = 7. Total: 48 + 7 = 55.
Both digits are ordinary numerals (0-9), so no conversion from letter to value is needed before multiplying by the place value.
Two Digit - Letter Digits Included
Convert B4 to decimal.
| 16 | 1 |
|---|---|
| B | 4 |
B = 11 in decimal. Left digit: 11 × 16 = 176. Right digit: 4 × 1 = 4. Total: 176 + 4 = 180.
The critical step is converting the letter to its decimal value before multiplying by the place value. B means 11, not the letter B - then multiply by 16 as normal.
Deep Dive: Converting Decimal to Hexadecimal
To convert a decimal number (0-255) to hexadecimal, divide by 16 and use the quotient and remainder as the two hex digits. The quotient (how many 16s fit) is the left digit; the remainder (what is left over) is the right digit. If either digit is 10 or above, replace it with the corresponding letter (10=A, 11=B, 12=C, 13=D, 14=E, 15=F).
Example: convert 200 to hex. 200 ÷ 16 = 12 remainder 8. Left digit = 12 = C. Right digit = 8. Answer: C8. Check: 12 × 16 + 8 = 192 + 8 = 200. ✓
Example: convert 255 to hex. 255 ÷ 16 = 15 remainder 15. Left digit = 15 = F. Right digit = 15 = F. Answer: FF. Check: 15 × 16 + 15 = 240 + 15 = 255. ✓
Key Takeaways
- Hexadecimal uses 16 symbols: digits 0-9 and letters A-F (A=10 through F=15).
- The two column place values for two-digit hex are 16 (left) and 1 (right). To convert to decimal: multiply each digit by its place value and add.
- Two hex digits cover exactly 0 to 255 (
00toFF) - the same range as one byte. - To convert decimal to hex: divide by 16 - the quotient is the left digit, the remainder is the right digit. Replace any value of 10 or above with the appropriate letter.
- Always convert a letter digit to its decimal value (e.g. D = 13) before multiplying by its place value.