2.5 Base Conversion: Decimal, Binary, Octal, and Hexadecimal Interconversion

Convert whole and fractional decimal numbers into binary using the division-remainder and multiplication methods, and convert freely between binary, octal, and hexadecimal.

Numbers do not change in value when they change representation — 45 decimal, 101101 binary, 55 octal, and 2D hexadecimal are all exactly the same quantity, just written in different bases. Base conversion is the set of systematic algorithms for moving a value between these representations, and it is one of the most frequently exercised mechanical skills in digital electronics, needed constantly when translating between how a human specifies a number and how a machine stores or displays it.

Decimal to binary: repeated division

To convert a whole decimal number to binary, repeatedly divide the number by 2, recording the remainder (0 or 1) at each step, until the quotient reaches 0. Reading the remainders in reverse order — from the last one obtained back to the first — gives the binary representation, with the last remainder becoming the MSB and the first remainder becoming the LSB.

This division-by-the-target-base method generalizes directly to any target base: converting decimal to octal uses repeated division by 8, and converting decimal to hexadecimal uses repeated division by 16, recording each remainder as one digit of the new-base result, read in reverse.

Binary to decimal: the weighted-sum method

Converting the other direction — binary back to decimal — uses the positional weighted-sum formula from the previous topics: multiply each bit by its power-of-2 weight and add the results. This is generally more direct as arithmetic than as a repeated-operation algorithm, since it is a single sum rather than an iterative process.

Binary <-> octal and binary <-> hexadecimal

Converting between binary and octal, or between binary and hexadecimal, does not require going through decimal at all — these conversions use the direct bit-grouping method covered in the previous two topics: group binary bits in sets of 3 for octal, or sets of 4 for hexadecimal, and translate each group independently (or expand each octal/hex digit back into its fixed-width binary group for the reverse direction).

Converting fractional decimal numbers

The fractional part of a decimal number is converted to a target base by the mirror-image method of the whole-number algorithm: repeatedly multiply the fractional part by the target base, and at each step record the integer part of the result (which will be a valid digit in the target base) before discarding it and continuing with the new fractional remainder. Unlike the whole-number method, the digits are read in the order they are produced (not reversed), and the process may not terminate exactly for some fractions, requiring a stopping precision to be chosen.

Conversion algorithms

The whole-number algorithm reads remainders in reverse; the fractional algorithm reads integer parts in forward order — mixing these up is a common source of errors. A terminating decimal fraction does not always terminate exactly in binary (e.g., 0.1 decimal repeats forever in binary), which is a foundational reason floating-point arithmetic has rounding error.

Decimal whole number -> binary (or any base b):
  repeat: divide by b, record remainder, keep dividing the quotient
  until quotient = 0; read remainders in REVERSE order (last to first)

Decimal fraction -> binary (or any base b):
  repeat: multiply fraction by b, record the integer part
  keep the new fractional part and repeat;
  read the recorded integer parts in the ORDER produced (not reversed)

Binary <-> octal:    group/expand in sets of 3 bits
Binary <-> hex:      group/expand in sets of 4 bits

Code Examples

Each division by 2 peels off one bit: the remainder at each step (0 or 1) is one binary digit, with the very first remainder produced becoming the least significant bit and the very last remainder (once the quotient hits 0) becoming the most significant bit. Reversing the order of the recorded remainders gives the final binary result.

45_10 -> 101101_2 (repeated division by 2, remainders read in reverse)

Each multiplication by 2 shifts the next binary fractional digit into the integer position; the digit is recorded and only the new fractional remainder is carried forward. Because 0.625 = 1/2 + 1/8 exactly, this particular fraction terminates cleanly after 3 steps, but many decimal fractions do not terminate and must be truncated to a chosen number of bits.

0.625_10 = 0.101_2 (multiply-by-base method, digits read forward)

45 / 2 = 22 remainder 1   (LSB)
22 / 2 = 11 remainder 0
11 / 2 =  5 remainder 1
 5 / 2 =  2 remainder 1
 2 / 2 =  1 remainder 0
 1 / 2 =  0 remainder 1   (MSB)

Read remainders bottom-to-top: 101101

45_10 = 101101_2
Convert 0.625 (fraction only) to binary:

0.625 x 2 = 1.25   -> integer part 1
0.25  x 2 = 0.50   -> integer part 0
0.50  x 2 = 1.00   -> integer part 1
(fraction is now 0 -- process terminates exactly)

Read integer parts in the order produced: 1, 0, 1

0.625_10 = 0.101_2

Conversion routes between the four common bases

Decimal converts to/from any base via division (to) and weighted sum (from); binary converts to/from octal and hex via direct bit grouping, no decimal step required.

repeated division by target base
 Decimal  ------------------------------>  Binary / Octal / Hex
         <------------------------------
              weighted-sum (Sum di x base^i)

   Binary  <---- group in 3s ---->  Octal
   Binary  <---- group in 4s ---->  Hex

Important Rules

Decimal-to-base-b whole numbers: repeatedly divide by b, record remainders, read them in reverse (last remainder = MSB).

Base-b-to-decimal: apply the weighted-sum formula, N = Sum(di x b^i).

Decimal fractions to base b: repeatedly multiply the fractional part by b, record integer parts, read them in forward order.

Binary <-> octal and binary <-> hex never need to pass through decimal — use direct 3-bit or 4-bit grouping instead.

Where this shows up

Base conversion is exercised every time a programmer writes a hex literal that a compiler must store as binary, every time a debugger displays a raw memory word as decimal for human readability, and every time a hardware engineer computes an address offset by hand while reading a memory map expressed in hexadecimal.

Reading the remainders of a repeated-division conversion in the same order they were produced, instead of reversing them.

Reverse the order of remainders before writing the final binary/octal/hex result — the first remainder obtained is the LSB, and the last is the MSB.

The division-remainder algorithm naturally produces the least-significant digit first, so failing to reverse the recorded remainders before reading them off produces a completely wrong (bit-reversed) result.

[tricky] When converting a fractional decimal number to binary, how should the recorded digits be ordered? A. In reverse order, just like whole-number conversion B. In the order they were produced, since the multiply-by-base method generates the most significant fractional bit first C. Alphabetically D. It does not matter, since fractions have no positional significance

B. In the order they were produced, since the multiply-by-base method generates the most significant fractional bit first — For fractional decimal numbers, repeatedly multiply the fractional part by the target base and record the integer part produced at each step; unlike whole-number division, these digits are read off in the forward order they are generated, because the multiplication method produces the most significant fractional digit first.

[interview] Why can binary and hexadecimal be converted directly into each other without going through decimal? A. They cannot — decimal is always required as an intermediate step B. Because 16 is a power of 2 (2^4), so each hex digit corresponds to exactly one fixed-width group of 4 binary bits, making the conversion a direct grouping/lookup rather than an arithmetic operation C. Because hexadecimal numbers are secretly stored as decimal internally D. Because binary and hexadecimal use the same digit symbols

B. Because 16 is a power of 2 (2^4), so each hex digit corresponds to exactly one fixed-width group of 4 binary bits, making the conversion a direct grouping/lookup rather than an arithmetic operation — Since 16 = 2^4, every possible 4-bit binary pattern corresponds to exactly one hexadecimal digit with no remainder, so binary and hex can be translated directly, digit-group by digit-group, with no arithmetic conversion through decimal required — the same principle applies to octal with 3-bit groups.

Convert decimal 0.1875 to binary using the multiplication method, and separately convert binary 11001010 directly to hexadecimal (without going through decimal).

0.1875 x 2 = 0.375 (0); 0.375 x 2 = 0.75 (0); 0.75 x 2 = 1.5 (1); 0.5 x 2 = 1.0 (1) -- fraction now 0, done. Reading forward: 0.1875_10 = 0.0011_2. For the hex conversion: 11001010 splits into 1100 (C) and 1010 (A), giving 0xCA.

For the fraction, stop once the fractional part reaches exactly 0. For the hex conversion, group the binary into 4-bit nibbles.