Decimal to hex
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 8 | 1000 | 10 | 8 |
| 10 | 1010 | 12 | A |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
| 64 | 1000000 | 100 | 40 |
| 100 | 1100100 | 144 | 64 |
| 255 | 11111111 | 377 | FF |
| 256 | 100000000 | 400 | 100 |
| 1024 | 10000000000 | 2000 | 400 |
| 65535 | 1111111111111111 | 177777 | FFFF |
To convert decimal to hexadecimal, divide repeatedly by 16 and read the remainders backwards. 255 gives 15 remainder 15, which is F and F, so FF. Values 10 to 15 are written A to F.
How to convert decimal to hex
The repeated-division method works for any base and is worth understanding rather than memorising. Divide by the target base, note the remainder, divide the quotient again, and continue until the quotient is zero: then read the remainders bottom to top. 48879 divided by 16 gives 3054 remainder 15, then 190 remainder 14, then 11 remainder 14, then 0 remainder 11: F, E, E, B read backwards is BEEF.
Hexadecimal has no fixed width, and that is where this conversion usually goes wrong. 12 comes out as C, one digit, but a colour channel or a byte field wants two: 0C, padded on the left. The CSS three-digit shorthand is not that padding: #f80 means #ff8800, each digit doubled, so you cannot shorten a value by dropping digits from it.
Two limits worth stating. A decimal point is refused rather than truncated, because a hex fraction is a different notation and quietly dropping the part after the point would give an answer that looks right. And a negative number is written with a minus sign in front of the magnitude, which is how a person writes it. The 32-bit pattern a computer actually stores for −255 is FFFFFF01, which is the two’s complement calculator’s job rather than this one’s.
Questions
FF. It is the maximum value of one byte.
Divide by 16 repeatedly and read the remainders in reverse, writing 10 to 15 as A to F.
3E8, that is 3×256 + 14×16 + 8.
Either works. Uppercase is more common for values, lowercase for colour codes in CSS.
Two per byte. A 32-bit value is eight hex digits, and a 24-bit colour is six.
Two digits, padded on the left: 12 is 0C and not C0. Getting that backwards multiplies the channel by 16.
It writes −FF for −255, the magnitude with a sign. If you want FFFFFF01. The pattern stored in 32 bits. Use the two’s complement calculator.