Converting numbers between number systems is a basic concept in programming.
One common need is converting octal numbers (base-8) to decimal numbers (base-10) — especially when working with file permissions in Unix/Linux or low-level memory operations.
What is an Octal Number?
An octal number uses base-8, meaning it only includes digits from 0 to 7. Each position represents a power of 8, starting from the right.For example-- Octal
71
→ Decimal57
- Octal
204
→ Decimal132
- Octal
345
→ Decimal229
🧮 How Octal to Decimal Conversion Works
The general formula -decimal = (digit_n * 8^n) + (digit_n-1 * 8^n-1) + ... + (digit_0 * 8^0)
71 (octal)
-= 7 * 8^1 + 1 * 8^0= 56 + 1 = 57
🐍 Method 1: Manual Conversion Using Loop (Python)
We’ll extract each digit, multiply it by the corresponding power of 8, and add it to the result.✅ Python Code -def octal_to_decimal(octal): decimal = 0 base = 1 while octal > 0: last_digit = octal % 10 decimal += last_digit * base base *= 8 octal //= 10 return decimal# Exampleoctal_num = 204print(f"Decimal of {octal_num} is {octal_to_decimal(octal_num)}") # Output: 132
- O(log N) – where N is the octal number
- Space Complexity: O(1)
Method 2: Using int() Built-in Function
Python makes life easy. The int() function can convert strings in any base.✅ Code Example -octal_str = "345"decimal = int(octal_str, 8)print(f"Decimal of {octal_str} is {decimal}") # Output: 229
Method 3: Using Recursion
You can also use recursion to process digits from right to left.✅ Python Code -def recursive_octal_to_decimal(octal): if octal == 0: return 0 return (octal % 10) + 8 * recursive_octal_to_decimal(octal // 10)# Exampleoctal_num = 132print(f"Decimal of {octal_num} is {recursive_octal_to_decimal(octal_num)}") # Output: 90
- Breaks down the number by digits
- Multiplies each digit by increasing powers of 8
- Recursively adds the results
Summary Table Using Python
Method | Best Use Case | Time Complexity |
Manual loop | Custom logic or learning purpose | O(log N) |
Built-in int() | Fast and clean scripts | O(1) |
Recursion | Recursive learning practice | O(log N) |
💬 Final Thoughts
Octal to decimal conversion might sound technical at first, but with just a bit of logic or the help of Python’s built-in features, it becomes a breeze.
Whether you're handling file permissions, reading binary data, or just brushing up on number systems, these methods will serve you well.
👉 Also Read:
- Beginner’s Guide to Django Models and Forms
- Top 10 Highest-Paying Freelance Jobs in 2025