How hexadecimal works
Hexadecimal, or hex, is base 16. It needs sixteen digits, so after 0 to 9 it borrows the letters A to F for the values 10 to 15. Each place is worth sixteen times the one to its right.
Value = dn × 16n + … + d2 × 256 + d1 × 16 + d0 × 1
Each d is one hex digit and its subscript is its position, counted from 0 at the right. So 2F is 2 × 16 + 15 = 47, and 1A3 is 1 × 256 + 10 × 16 + 3 = 419. Hex is popular in computing because 16 is 24: one hex digit stands for exactly four binary digits, so a byte of eight bits is always two hex digits. FF is 1111 1111, which is 255.
Decimal to hex: divide by 16 repeatedly and read the remainders from last to first
Worked examples
Converting 48,879 to hex
- 48,879 ÷ 16 = 3,054 remainder 15, which is F
- 3,054 ÷ 16 = 190 remainder 14, which is E
- 190 ÷ 16 = 11 remainder 14, which is E
- 11 ÷ 16 = 0 remainder 11, which is B
Reading the remainders upwards gives BEEF.
Adding 2F and 3C
- Right-hand column. F + C is 15 + 12 = 27. That is one sixteen and 11 left over, so write B and carry 1.
- Next column. 2 + 3 + the carried 1 = 6. Write 6.
The answer is 6B. In decimal the same sum is 47 + 60 = 107, and 6 × 16 + 11 = 107. The only difference from decimal addition is that you carry when a column reaches 16 instead of 10.
The other operations follow the same pattern, or you can convert to decimal, calculate, and convert back, which is what the working on this page shows. 3C − 2F = D (13). FF × FF = FE01 (255 × 255 = 65,025). Hex 100 ÷ 7 = 24 remainder 4, because 256 = 36 × 7 + 4 and 36 is 24 in hex.
Hex colour codes
Screens mix colours from red, green and blue light, each set to a level from 0 to 255. A hex colour code writes those three levels as three two-digit hex numbers in a row, after a # sign.
#RRGGBB: red = RR, green = GG, blue = BB, each from 00 (0) to FF (255)
For #0B6B5D, the green used on this site: 0B is 0 × 16 + 11 = 11, 6B is 6 × 16 + 11 = 107, and 5D is 5 × 16 + 13 = 93, so the colour is rgb(11, 107, 93). Going the other way, divide each value by 16: 107 ÷ 16 = 6 remainder 11, giving 6B.
Three levels of 256 steps each give 256 × 256 × 256 = 16,777,216 possible colours. A few variations are worth knowing:
- Three-digit shorthand. #F80 means #FF8800. Each digit is doubled, not followed by a zero, so F becomes FF (255) and 8 becomes 88 (136). The colour is rgb(255, 136, 0).
- Eight digits. #RRGGBBAA adds an alpha pair for opacity, where 00 is fully transparent and FF is solid. 80 is 128, or about 50%.
- Greys. When all three pairs are equal the colour is a shade of grey, from #000000 (black) to #FFFFFF (white).
The calculator also reports the contrast ratio of white and of black text on the colour, using the formula in the Web Content Accessibility Guidelines. Body text should reach at least 4.5 to 1. White on #0B6B5D scores about 6.4 to 1, so it passes, while white on #FF8800 scores about 2.4 to 1 and does not.
Common mistakes
- Reading hex 10 as ten. In hex, 10 is sixteen, and 100 is 256. Programmers write a 0x prefix (0x10) to make the base clear. The calculator accepts numbers with or without it.
- Mixing up similar characters. The letter O is not a hex digit, and the digit 0 is. Only A to F are used, and upper and lower case mean the same thing.
- Carrying at ten. 9 + 1 is A, not 10. A column only carries when it reaches 16.
- Splitting a colour code in the wrong place. The pairs are fixed: the first two digits are always red. Dropping a leading zero, as in writing #B6B5D, shifts every pair and gives a different colour or an invalid code.
- RGB values above 255. Two hex digits cannot hold more than FF. A value such as 300 has no hex colour pair.
This page works with whole numbers. A negative result, such as 2F − 3C, is shown with a minus sign (−D). To see how a computer would store it, use the two’s complement mode of the binary calculator.