2.2 Binary, Octal, Decimal, and Hexadecimal Conversion
The previous section defined a numeral as a sum of digit-times-weight contributions. Conversion does not change the value; it changes only the symbols used to describe that value.
For example:
All four numerals identify the same quantity.
Choose a conversion route
Different source and destination bases suggest different tools.
Route 1: expand a numeral into decimal
Use the place-value definition directly:
The hexadecimal digit D contributes thirteen times its weight, not the decimal digit sequence 13.
Horner's method performs the same calculation from left to right:
This form is useful in programs because it repeatedly multiplies the running total by the base and adds the next digit.
Route 2: group bits for octal and hexadecimal
Because , exactly three binary bits describe one octal digit. Because , exactly four bits describe one hexadecimal digit.
| Binary group | Octal | Binary group | Hex |
|---|---|---|---|
000 | 0 | 0000 | 0 |
001 | 1 | 0001 | 1 |
010 | 2 | 0010 | 2 |
011 | 3 | 0011 | 3 |
100 | 4 | 1010 | A |
101 | 5 | 1100 | C |
110 | 6 | 1110 | E |
111 | 7 | 1111 | F |
For hexadecimal, group from the radix point outward in sets of four:
| Binary group | 1010 | 0101 | 1100 |
|---|---|---|---|
| Hexadecimal digit | A | 5 | C |
Therefore .
For octal, regroup the same bits in threes:
| Binary group | 101 | 001 | 011 | 100 |
|---|---|---|---|---|
| Octal digit | 5 | 1 | 3 | 4 |
Therefore .
If the leftmost group is short, pad it with leading zeros. Padding does not change the value.
Lab 1 — translate one bit pattern three ways
Toggle a 12-bit field to meet hexadecimal, octal, and decimal missions. The lab redraws both 3-bit and 4-bit boundaries while preserving the same underlying bits.
After solving a mission, toggle the lowest bit and then the highest bit. Compare the numerical changes.
Route 3: repeated division for decimal integers
To convert a nonnegative decimal integer to base :
1. divide the current value by ;
2. record the remainder, which is the next least-significant digit;
3. continue with the quotient;
4. stop when the quotient is zero;
5. read the remainders in reverse production order.
Convert to hexadecimal:
| Division | Quotient | Remainder |
|---|---|---|
| 10 | 13 = D | |
| 0 | 10 = A |
The first remainder is the low digit, so read upward:
Check by expansion:
The value zero is a boundary case. Repeated division performs no positive-value iteration, but its representation is still 0.
Lab 2 — assemble remainders in the correct direction
Change the decimal input and destination base. The workshop generates all quotient/remainder rows, but you must select the remainder cards in the correct order.
Try a number whose conversion contains repeated digits. The cards have separate identities even when their digit symbols match.
Converting fractional parts
For a fraction, repeated multiplication is the mirror image of repeated division.
To convert decimal fraction to base :
1. multiply by ;
2. record the integer part as the next digit from left to right;
3. continue with the new fractional part.
Convert to binary:
| Multiplication | Integer digit | New fraction |
|---|---|---|
| 1 | 0.25 | |
| 0 | 0.5 | |
| 1 | 0 |
So .
Unlike integer remainders, fractional digits are already produced from most significant to least significant; do not reverse them.
Some fractions never terminate in the destination base. A fixed-width system must stop after a chosen number of digits and record that the result is approximate.
Checkpoint
Use a method that exposes a check:
- non-decimal to decimal: expand place values;
- decimal integer to base : repeated division and reverse remainders;
- decimal fraction to base : repeated multiplication and keep digit order;
- binary ↔ octal/hexadecimal: group bits around the radix point.
The next section asks a deeper question: after bits have been converted and stored, should the top bit represent a positive weight or a negative sign?