Number System Conversions
Conversion map
There are only two big methods. To go from decimal to another base, divide repeatedly. To come to decimal, multiply by place values and add. Between binary, octal and hex we use quick grouping shortcuts.
Decimal → binary, octal or hexadecimal
Rule: divide the decimal number by the new base, write the remainder, divide the quotient again, and continue until the quotient is 0. Read remainders from bottom to top.
Example 1: (156)₁₀ → binary
| Divide | Quotient | Remainder |
|---|---|---|
| 156 ÷ 2 | 78 | 0 |
| 78 ÷ 2 | 39 | 0 |
| 39 ÷ 2 | 19 | 1 |
| 19 ÷ 2 | 9 | 1 |
| 9 ÷ 2 | 4 | 1 |
| 4 ÷ 2 | 2 | 0 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
Example 2: (156)₁₀ → octal
156 ÷ 8 = 19 remainder 4; 19 ÷ 8 = 2 remainder 3; 2 ÷ 8 = 0 remainder 2. Upward: (234)₈.
Example 3: (156)₁₀ → hexadecimal
156 ÷ 16 = 9 remainder 12 = C; 9 ÷ 16 = 0 remainder 9. Upward: (9C)₁₆.
In hex, a remainder of 10–15 must be written as a letter A–F, not as two digits. Writing 156 as "912" is wrong; it is 9C.
Any base → decimal
Rule: multiply each digit by baseposition (position 0 at the right) and add all results.
| Number | Expansion | Decimal |
|---|---|---|
| (10011100)₂ | 1×128 + 0×64 + 0×32 + 1×16 + 1×8 + 1×4 + 0×2 + 0×1 | 156 |
| (234)₈ | 2×64 + 3×8 + 4×1 | 156 |
| (9C)₁₆ | 9×16 + 12×1 | 156 |
| (1011.1)₂ | 8 + 0 + 2 + 1 + 1×2⁻¹ | 11.5 |
Digits after a point have negative positions: −1, −2 … So in binary the weights are ½ = 0.5, ¼ = 0.25, ⅛ = 0.125 …
Decimal fractions → binary
Rule: multiply the fractional part by 2. The integer part (0 or 1) is the next bit. Keep only the fraction and repeat. Read bits top to bottom.
| Step | Multiply | Result | Bit |
|---|---|---|---|
| 1 | 0.625 × 2 | 1.25 | 1 |
| 2 | 0.25 × 2 | 0.5 | 0 |
| 3 | 0.5 × 2 | 1.0 | 1 |
| 4 | 0.0 – stop |
x = 0.625
bits = ''
for step in range(4):
x = x * 2
bits += str(int(x)) # integer part is the next bit
x = x - int(x)
print('0.' + bits)So (12.625)₁₀ = (1100.101)₂ — convert the integer part by division and the fraction by multiplication, then join them.
Binary ↔ octal (groups of 3)
Binary → octal: starting from the right, make groups of 3 bits (add 0s on the left if needed) and write each group's value.
Octal → binary: write each octal digit as 3 bits. (57)₈ → 101 111 → (101111)₂.
Binary ↔ hexadecimal (groups of 4)
Binary → hex: groups of 4 bits from the right. (10011100)₂ → 1001 1100 → 9 C → (9C)₁₆.
Hex → binary: each hex digit becomes 4 bits. (A7)₁₆ → 1010 0111 → (10100111)₂.
Octal ↔ hexadecimal
There is no direct shortcut, so use binary in the middle.
Reverse example: (B2)₁₆ → 1011 0010 → regroup 3s: 010 110 010 → (262)₈.
Write your own converter in Python
This program uses the same repeated-division method you do on paper — % gives the remainder, // gives the quotient.
def to_base(n, base):
digits = '0123456789ABCDEF'
if n == 0:
return '0'
result = ''
while n > 0:
result = digits[n % base] + result # remainder becomes next digit
n = n // base
return result
for b in (2, 8, 16):
print(156, 'in base', b, '=', to_base(156, b))And the reverse, using the place-value method (Horner's way: multiply the running value by the base and add the next digit):
def to_decimal(text, base):
digits = '0123456789ABCDEF'
value = 0
for ch in text.upper():
value = value * base + digits.index(ch)
return value
print(to_decimal('10011100', 2))
print(to_decimal('234', 8))
print(to_decimal('9C', 16))Summary & practice
- Decimal → base b: divide by b, remainders upward.
- Base b → decimal: Σ digit × bposition.
- Fraction → binary: multiply by 2, integer parts downward.
- Binary ↔ octal: groups of 3; binary ↔ hex: groups of 4; octal ↔ hex via binary.
1) (99)₁₀ = (?)₂ 2) (110110)₂ = (?)₁₆ 3) (725)₈ = (?)₁₀ 4) (3F)₁₆ = (?)₈ 5) (0.75)₁₀ = (?)₂
Answers: 1) 1100011 2) 36 3) 469 4) 77 5) 0.11
