Guide
How to convert decimal to hexadecimal step by step
Convert a decimal integer to hexadecimal by dividing by 16, interpreting remainders, and checking the exact result.
by Tools in a Tab · Published on · Reviewed on
Short answer
To convert a decimal integer to hexadecimal, divide it repeatedly by 16,
record each remainder, and read the remainders from last to first. Remainders
0 through 9 keep their digits; 10, 11, 12, 13, 14, and 15
become A, B, C, D, E, and F.
Example: convert 255 to hexadecimal
Only two divisions are needed:
| Division | Quotient | Remainder | Hexadecimal digit |
|---|---|---|---|
255 ÷ 16 |
15 |
15 |
F |
15 ÷ 16 |
0 |
15 |
F |
Reading the remainders from bottom to top gives:
255 decimal = FF hexadecimal
The 0x prefix commonly declares the base, so 0xFF represents the same
value. Check it with the
IPv4 and number base converter, which
uses exact integer arithmetic even for values beyond JavaScript’s safe Number
range.
Why division by 16 works
Hexadecimal is a positional base-16 system. Each place has a power-of-16 weight:
FF = 15 × 16¹ + 15 × 16⁰
= 240 + 15
= 255
The same process works for every non-negative integer. For example, 4095
produces three remainders of 15, so its result is FFF. Zero is a direct
case: decimal 0 is hexadecimal 0.
Common mistakes
- Reading remainders in the order they were produced instead of reversing them.
- Writing decimal
15as two hexadecimal characters instead ofF. - Treating decimal
10and hexadecimal10as equal:0x10is decimal16. - Including visual separators as though they were digits.
- Applying two’s complement before choosing a bit width.
A negative mathematical value can keep its sign, such as -26 = -1A. That is
not a two’s-complement binary encoding. An 8-, 16-, or 32-bit width must be
chosen first because each produces a different pattern; the converter does not
guess that width.
Check the conversion
Convert the hexadecimal result back by multiplying each digit by its power of 16. This reverse check catches misplaced remainders and incorrect digits. The
ECMAScript specification supports exact BigInt string representations with
an explicit radix from 2 through 36 in
BigInt.prototype.toString.