How to convert a decimal number to binary and hexadecimal

Converting a decimal integer to another base requires repeated division by the target base (2 for binary, 16 for hexadecimal). The remainders of these divisions, read in reverse order, form the converted number.

This method applies to any positive integer conversion from base-10 to an arbitrary target base.

The setup

Identify the decimal integer NN and the target base bb. For binary, b=2b = 2. For hexadecimal, b=16b = 16. Prepare a two-column workspace to record successive quotients and remainders.

The steps

  1. Divide the decimal number NN by the base bb using integer division.
  2. Record the integer quotient and the remainder.
  3. Replace NN with the new quotient.
  4. Repeat steps 1-3 until the quotient becomes 0.
  5. Read the remainders from bottom to top (last remainder computed is the most significant digit).
  6. For hexadecimal, convert any remainders from 10 to 15 into letters: 10=A,11=B,12=C,13=D,14=E,15=F10=A, 11=B, 12=C, 13=D, 14=E, 15=F.

Checking the result

Multiply each digit of your result by bib^i, where ii is the zero-indexed position of the digit starting from the right. Sum these products. The total must equal your original decimal number NN.

Common errors

Reading the remainders top-to-bottom instead of bottom-to-top, resulting in a reversed string. Forgetting to map hexadecimal remainders 101510-15 to AFA-F. Stopping the division process when the quotient is 1 instead of continuing until it is exactly 0.

Worked example

Convert the decimal number 254254 to binary and hexadecimal.

Binary Conversion (b=2b=2) 254÷2=127254 \div 2 = 127 remainder 00 127÷2=63127 \div 2 = 63 remainder 11 63÷2=3163 \div 2 = 31 remainder 11 31÷2=1531 \div 2 = 15 remainder 11 15÷2=715 \div 2 = 7 remainder 11 7÷2=37 \div 2 = 3 remainder 11 3÷2=13 \div 2 = 1 remainder 11 1÷2=01 \div 2 = 0 remainder 11 Reading bottom to top, the binary representation is 11111110211111110_{2}.

Hexadecimal Conversion (b=16b=16) 254÷16=15254 \div 16 = 15 remainder 1414 15÷16=015 \div 16 = 0 remainder 1515 Convert remainders to hex digits: 14=E14 = E, 15=F15 = F. Reading bottom to top, the hexadecimal representation is FE16FE_{16}.

FAQ

Run your own problem

References: Digital Design by M. Morris Mano · Computer Organization and Design by David A. Patterson and John L. Hennessy

See also