Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: Decimal and Hexadecimal

Converting from Decimal to Hex

To convert from decimal to hex, pretend you are making change.

Example To convert \(99_\text{DEC}\) to hex, we first ask how many 16’s it contains. We compute \(6 \cdot 16 = 96\), with remainder 3.

\[ \begin{aligned} 99\,_\text{DEC} &= 6 \cdot 16 + 3 \\ &= \text{0x}63 \\ \end{aligned} \]

Example To convert \(300_\text{DEC}\) to hex, we first ask how many 256’s it contains (one, with remainder 44). Then we ask how many 16’s are contained in 44 (2, with remainder 12).

\[ \begin{aligned} 300\,_\text{DEC} &= 1 \cdot 256 + 2 \cdot 16 + 12 \\ &= \text{0x}12C \\ \end{aligned} \]


Next: