Converting decimal to hexadecimal is the reverse of hex-to-decimal, and it’s a core skill for programmers, network engineers, and anyone working with color codes or memory addresses. Here’s the reliable method with worked examples.
The Method: Divide by 16, Keep Remainders
Step 1. Divide the decimal number by 16.
Step 2. Write down the remainder (convert 10-15 to A-F).
Step 3. Repeat with the quotient until the quotient is 0.
Step 4. Read the remainders bottom to top – that’s your hex number.
Worked example: 759 to hex
“`
759 ÷ 16 = 47 remainder 7 → 7
47 ÷ 16 = 2 remainder 15 → F
2 ÷ 16 = 0 remainder 2 → 2
“`
Read bottom to top: 2F7. Check: (2×256) + (15×16) + 7 = 512 + 240 + 7 = 759 ✓
More Worked Examples
Example 1: 255 to hex
“`
255 ÷ 16 = 15 remainder 15 → F
15 ÷ 16 = 0 remainder 15 → F
“`
FF – the maximum single byte, as expected.
Example 2: 100 to hex
“`
100 ÷ 16 = 6 remainder 4 → 4
6 ÷ 16 = 0 remainder 6 → 6
“`
64. Verify: (6×16) + 4 = 96 + 4 = 100 ✓
Example 3: 420 to hex
“`
420 ÷ 16 = 26 remainder 4 → 4
26 ÷ 16 = 1 remainder 10 → A
1 ÷ 16 = 0 remainder 1 → 1
“`
1A4. Verify: 256 + 160 + 4 = 420 ✓
Example 4: 4096 to hex
“`
4096 ÷ 16 = 256 remainder 0 → 0
256 ÷ 16 = 16 remainder 0 → 0
16 ÷ 16 = 1 remainder 0 → 0
1 ÷ 16 = 0 remainder 1 → 1
“`
1000. Makes sense: 4096 = 16³, so it’s a 1 followed by three zeros in hex – just like 1000 = 10³ in decimal.
The Shortcut: Decimal → Binary → Hex
If the divide-by-16 method feels clunky, go through binary:
- Convert decimal to binary (divide by 2, keep remainders).
- Group the binary digits in fours from the right.
- Convert each group to its hex digit.
Example with 759:
- 759 in binary = 1011110111
- Group: 10 1111 0111 → pad to 0010 1111 0111
- Convert: 0010=2, 1111=F, 0111=7 → 2F7 ✓
Our binary converter does all three representations at once – type a decimal number and see its binary and hex instantly, which is great for checking your hand calculations.
Common Decimal-to-Hex Values
| Decimal | Hex | Notes |
|---|---|---|
| 10 | A | First letter digit |
| 15 | F | Max single hex digit |
| 16 | 10 | Hex “ten” |
| 100 | 64 | |
| 128 | 80 | Half of 256 |
| 255 | FF | Max one byte |
| 256 | 100 | 16² |
| 1000 | 3E8 | |
| 4096 | 1000 | 16³ |
| 65535 | FFFF | Max 16-bit value |
Decimal to Hex in Programming
Every major language converts decimal to hex natively:
Python:
“`python
hex(759) # → ‘0x2f7’
hex(255) # → ‘0xff’
“`
JavaScript:
“`javascript
(759).toString(16); // → “2f7”
(255).toString(16); // → “ff”
“`
Formatting with leading zeros: Byte values are conventionally written as two hex digits, so decimal 10 becomes `0A`, not `A`. In Python: `format(10, ’02X’)` → `’0A’`. In JavaScript: `(10).toString(16).padStart(2, ‘0’)` → `”0a”`. This matters for color codes (`#0A0A0A` vs the invalid `#AAA` interpretation) and for aligning hex dumps.
Spreadsheet: `=DEC2HEX(759)` in Excel or Google Sheets gives `2F7`. The optional second argument sets minimum digits: `=DEC2HEX(10, 4)` gives `000A`.
Knowing the manual divide-by-16 method helps you read unfamiliar code, estimate magnitudes (a 6-digit hex number is in the millions), and debug format-string issues where `%x` vs `%d` changes output completely.
Practical Uses
- CSS colors: To write the color rgb(255, 87, 51) as hex, convert each: 255→FF, 87→57, 51→33, giving `#FF5733`.
- Memory addresses: Debuggers display addresses in hex because they’re compact.
- Subnet masks: 255.255.255.0 in an IP context is FFFFFF00 in hex.
- Error codes: That `0x0000007B` on a blue screen is just a decimal number written in hex.
Mistakes to Avoid
- Reading remainders top to bottom. The first remainder you compute is the *least* significant digit (rightmost). Always read bottom to top.
- Forgetting to convert remainders 10-15 to letters. A remainder of 13 is D, not 13.
- Stopping too early. Keep dividing until the quotient itself is 0 – the final quotient becomes your leftmost digit.
Frequently Asked Questions
How do I convert decimal to hexadecimal without a calculator?
Use the divide-by-16 method: divide by 16, record each remainder (10→A through 15→F), repeat with each quotient until you reach 0, then read the remainders from last to first.
What is 255 in hexadecimal?
FF. It’s (15×16) + 15 = 255, the largest value that fits in one byte.
How do I convert an RGB color to hex?
Convert each of the three values (0-255) to two-digit hex separately, then concatenate. For rgb(255, 87, 51): 255→FF, 87→57, 51→33, giving #FF5733. Try verifying with our binary converter.
Why do programmers write 0x before hex numbers?
The `0x` prefix tells the compiler (and readers) that the digits are hexadecimal, not decimal. `0x10` is 16; plain `10` is ten.