What is hexadecimal?
Hexadecimal, or hex, is the base 16 number system. It needs sixteen digit symbols, so after 0 through 9 it borrows the letters A through F for the values ten through fifteen. Each place is worth sixteen times the place to its right: 1, 16, 256, 4,096 and so on. Because one hex digit stands for exactly four binary digits, hex is a compact way to write binary data. A byte is always two hex digits.
| Hex | Decimal | Binary |
|---|---|---|
| 0 | 0 | 0000 |
| 5 | 5 | 0101 |
| 9 | 9 | 1001 |
| A | 10 | 1010 |
| B | 11 | 1011 |
| C | 12 | 1100 |
| D | 13 | 1101 |
| E | 14 | 1110 |
| F | 15 | 1111 |
| 10 | 16 | 10000 |
| 3F | 63 | 111111 |
Hex to decimal
Multiply each digit by 16 raised to its position (counting from 0 on the right) and add. For 2AA:
Another check: hex 1024 is 1×4096 + 0×256 + 2×16 + 4 = 4,132.
Decimal to hex
Divide by 16 repeatedly and keep the remainders, converting 10 to 15 into A to F. The remainders read from last to first are the hex digits. For 1,500: 1,500 ÷ 16 = 93 r 12 (C), 93 ÷ 16 = 5 r 13 (D), 5 ÷ 16 = 0 r 5. Reading upward gives 5DC.
Hex addition
Add column by column from the right. If a column sum reaches 16 or more, subtract 16, write the result and carry 1. For example, B + 7 = 18 in decimal, which is 12 hex: write 2 and carry 1. Adding two longer values works exactly as in decimal, only with 16 in place of 10.
Hex subtraction
Subtract each column, borrowing 16 from the next column to the left when the top digit is smaller. Example: 5DC − 3AF. In the right column C (12) − F (15) cannot be done, so borrow 16 to get 28 − 15 = 13, which is D. The middle column is then 13 − 1 (borrowed) − 10 = 2, and the left column is 5 − 3 = 2. The answer is 22D, matching 1,500 − 943 = 557 in decimal.
Hex multiplication
Multiply the top number by each digit of the bottom number in turn, shift each partial product one place left per position, and add them in base 16. Multiplying by hex 10 simply appends a zero. Since hex digits go up to F, it helps to keep a multiplication table or to convert partial products through decimal.
Hex division
Long division works the same way as in decimal. For FACE ÷ 12, the quotient is DEF with remainder 0: 64,206 ÷ 18 = 3,567, which is DEF in hex. When the division is not exact the calculator gives a whole-number quotient and a remainder.
Where hex is used
- Web and design colors such as #FF8800, where each pair of digits is red, green or blue from 00 to FF.
- Memory addresses and machine code in debugging tools.
- MAC addresses, IPv6 addresses and character codes.
- Hash values and cryptographic keys.
Tips for working in hex
Memorize that A is 10, B is 11, C is 12, D is 13, E is 14 and F is 15, and that F plus 1 rolls over to 10. Two hex digits cover one byte, so FF is 255, the largest byte value. Converting between hex and binary is a digit-by-digit lookup: 2AA is 0010 1010 1010, which is the binary number 1010101010 with leading zeros dropped. Prefixes such as 0x or # mark a value as hexadecimal in code and in web colors, but the digits themselves are the same.