Objective

Convert between binary, octal, decimal, and hexadecimal number systems and understand how these representations apply to file permissions, IP addressing, and memory.

Tools & Technologies

  • bc
  • printf
  • Python
  • binary arithmetic

Key Commands

echo 'obase=16; 255' | bc
printf '%x\n' 255
printf '%o\n' 255
python3 -c 'print(bin(255))'

Architecture Overview

flowchart TD DEC[Decimal\n255] -->|÷2 remainders| BIN[Binary\n11111111] DEC -->|÷8 remainders| OCT[Octal\n377] DEC -->|÷16 remainders| HEX[Hexadecimal\nFF] BIN -->|group 3 bits| OCT BIN -->|group 4 bits| HEX style DEC fill:#1a1a2e,stroke:#00d4ff,color:#e0e0e0 style BIN fill:#1a1a2e,stroke:#00ff88,color:#e0e0e0 style OCT fill:#1a1a2e,stroke:#ffd700,color:#e0e0e0 style HEX fill:#1a1a2e,stroke:#ff4444,color:#e0e0e0

Step-by-Step Process

01
Binary (Base 2)

Binary uses only 0 and 1. Each position is a power of 2. Used in CPU instructions and file permissions.

# Convert decimal to binary
echo 'obase=2; 42' | bc
# Result: 101010
# Verify: 32+8+2 = 42

# Python
python3 -c 'print(bin(42))'   # 0b101010
python3 -c 'print(0b101010)' # 42
02
Octal (Base 8)

Octal uses digits 0-7. File permissions (chmod 755) use octal — 3 bits per digit maps perfectly to rwx.

# chmod 755 means:
# 7 = 111 = rwx (owner)
# 5 = 101 = r-x (group)
# 5 = 101 = r-x (others)

printf '%o\n' 255   # 377
echo 'obase=8; 255' | bc
graph LR subgraph chmod 755 O[7\nooo] -->|binary| B1[111\nrwx] G[5\nggg] -->|binary| B2[101\nr-x] W[5\nwww] -->|binary| B3[101\nr-x] end
03
Hexadecimal (Base 16)

Hex uses 0-9 and A-F. Used in memory addresses, colours, and network hardware (MAC addresses).

printf '%x\n' 255    # ff
printf '%X\n' 255    # FF
python3 -c 'print(hex(255))'  # 0xff

# MAC address is 6 hex bytes:
# 00:1A:2B:3C:4D:5E
04
Conversion Practice

Use the shell as a calculator for all base conversions.

# Decimal → any base
echo 'obase=BASE; NUMBER' | bc

# Any base → decimal
echo 'ibase=16; FF' | bc   # 255
echo 'ibase=2; 1010' | bc  # 10

# Python one-liners
python3 -c 'print(int("FF",16))'  # 255
python3 -c 'print(int("1010",2))' # 10

Challenges & Solutions

  • bc obase/ibase must be set before the number when chaining
  • Capital letters required for hex input to bc

Key Takeaways

  • Hex and binary convert cleanly — every hex digit is exactly 4 bits
  • chmod permissions are octal — always think in groups of 3 bits