Convert Octal to Binary in C++

Convert Octal to Binary in C++ (5 Programs).

IntermediateAdvanced Number ProgramsExample 10 of 13
convert-octal-to-binary.cpp
1#include <iostream>
2#include <cmath>
3using namespace std;
4
5int octalToDecimal(long long octal) {
6 int decimal = 0, i = 0, remainder;
7 while (octal != 0) {
8 remainder = octal % 10;
9 octal /= 10;
10 decimal += remainder * pow(8, i);
11 ++i;
12 }
13 return decimal;
14}
15
16long long decimalToBinary(int decimal) {
17 long long binary = 0;
18 int remainder, i = 1;
19 while (decimal != 0) {
20 remainder = decimal % 2;
21 decimal /= 2;
22 binary += remainder * i;
23 i *= 10;
24 }
25 return binary;
26}
27
28int main() {
29 long long octal;
30 cout << "Enter an octal number: ";
31 cin >> octal;
32
33 int decimal = octalToDecimal(octal);
34 long long binary = decimalToBinary(decimal);
35
36 cout << "Octal: " << octal << " = Binary: " << binary << endl;
37
38 return 0;
39}

Output

Enter an octal number: 12
Octal: 12 = Binary: 1010

What's going on

The program converts an octal number to a binary number. For example:

Input octal: 12
Output binary: 1010

The program uses a two-step approach: first converts octal to decimal, then decimal to binary. This is straightforward and reuses the conversion algorithms we've already learned.

Example:

12 (octal) → 10 (decimal) → 1010 (binary)
17 (octal) → 15 (decimal) → 1111 (binary)

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <cmath>
Provides pow() function for calculating powers.
Used in the octal-to-decimal conversion.

Understanding the Two-Step Conversion

The program uses two helper functions:

Step 1: Octal to Decimal

Converts the octal number to decimal first.
Uses the algorithm: multiply each digit by 8^position and sum.

Step 2: Decimal to Binary

Converts the decimal result to binary.
Uses the algorithm: repeatedly divide by 2 and collect remainders.

Why Two Steps?

:

Simplest approach - reuses existing conversion algorithms.
Easy to understand and implement.
Can be optimized later with direct conversion.

Function 1: octalToDecimal()

int octalToDecimal(long long octal) {

int decimal = 0, i = 0, remainder;

while (octal != 0) {

remainder = octal % 10;

octal /= 10;

decimal += remainder * pow(8, i);

++i;

}

}

This function:

Takes an octal number as input.
Converts it to decimal using the standard algorithm.
Returns the decimal equivalent.
1return decimal;

How it works

(for octal = 12):

Extracts digits from right to left.
Multiplies each digit by 8^position.
Sums all contributions: 2×1 + 1×8 = 10

Function 2: decimalToBinary()

long long decimalToBinary(int decimal) {

long long binary = 0;

int remainder, i = 1;

while (decimal != 0) {

remainder = decimal % 2;

decimal /= 2;

binary += remainder * i;

i *= 10;

}

}

This function:

Takes a decimal number as input.
Converts it to binary using the standard algorithm.
Returns the binary equivalent.
1return binary;

How it works

(for decimal = 10):

Repeatedly divides by 2.
Collects remainders: 10÷2=5 rem 0, 5÷2=2 rem 1, 2÷2=1 rem 0, 1÷2=0 rem 1.
Builds binary: 0 + 1×10 + 0×100 + 1×1000 = 1010

Main Function - Combining Both Steps

long long octal;

cout << "Enter an octal number: ";

cin >> octal;

int decimal = octalToDecimal(octal);

long long binary = decimalToBinary(decimal);

cout << "Octal: " << octal << " = Binary: " << binary << endl;

return 0;

}

1int main() {

Process Flow

:

1.User enters octal number (e.g., 12).
2.Convert octal to decimal (12 → 10).
3.Convert decimal to binary (10 → 1010).
4.Display the result.

Direct Conversion Method (More Efficient)

Why Direct Conversion?

:

Two-step method works but is less efficient.
Direct conversion converts each octal digit to 3 binary digits.
More efficient: O(log n) vs O(log n) + O(log n).

How Direct Conversion Works

:

Each octal digit (0-7) converts directly to 3 binary digits.
Use conversion table for each digit.
Concatenate all binary groups.

Example

(octal 12 to binary):

Octal digit 2 → 010 (binary)
Octal digit 1 → 001 (binary)
Concatenate: 001010 → remove leading zeros → 1010 (binary)

Why 1 Octal Digit = 3 Binary Digits?

:

Octal is base-8, binary is base-2.
2³ = 8, so 1 octal digit needs 3 binary digits to represent 0-7.
This is the mathematical relationship between octal and binary.

Other Methods (Mentioned but not shown in code)

Method 2: Direct Conversion

Converts each octal digit to 3 binary digits.
Uses conversion table: 0→000, 1→001, 2→010, ..., 7→111.
More efficient than two-step method.

Method 3: Using bitset

Uses bitset for binary representation.
Converts each digit and combines.
1#include <bitset>
2// Convert each octal digit to binary using bitset
3// Concatenate results

Method 4: Using String Manipulation

Converts octal to string.
Processes each character (octal digit).
Converts each to 3-digit binary string.
Concatenates all binary strings.

Method 5: Using Functions (Modular)

Encapsulates each conversion step in functions.
Makes code more organized and testable.
Easier to maintain and debug.

Displaying the Result

The program prints:

Output:

Octal: 12 = Binary: 1010

This clearly shows the conversion from octal to binary.

1cout << "Octal: " << octal << " = Binary: " << binary << endl;

Understanding Octal-Binary Relationship

Why 1 Octal Digit = 3 Binary Digits?

:

Octal is base-8, binary is base-2.
2³ = 8, so 1 octal digit can represent 0-7, which requires 3 binary digits.
This makes direct conversion straightforward.

Conversion Table

(octal to 3-bit binary):

0 (octal) = 000 (binary)
1 (octal) = 001 (binary)
2 (octal) = 010 (binary)
3 (octal) = 011 (binary)
4 (octal) = 100 (binary)
5 (octal) = 101 (binary)
6 (octal) = 110 (binary)
7 (octal) = 111 (binary)

Direct Conversion Example

:

Octal: 1 2

Binary: 001 010

Result: 001010 → remove leading zeros → 1010 (binary)

When to Use Each Method

-

Two-Step Conversion

: Best for learning - clear, reuses known algorithms.

-

Direct Conversion

: Best for efficiency - faster, converts digits directly.

-

bitset Method

: Good for working with binary data - uses C++ bitset class.

-

String Manipulation

: Flexible - good for very large numbers.

-

Functions

: Best for code organization - modular and maintainable.

Best Practice

: Use two-step for learning, direct conversion for efficiency.

Important Considerations

Leading Zeros in Binary

:

When converting each octal digit, pad to 3 binary digits.
Example: 2 (octal) → 010 (binary), not 10.
Final result may have leading zeros that can be removed.

Large Octal Numbers

:

Very large octal numbers may cause overflow in intermediate steps.
Consider using string-based methods for very large numbers.
Direct conversion is more efficient for large numbers.

Validation

:

Ensure octal input contains only digits 0-7.
This program assumes valid octal input.
Invalid digits (8-9) would produce incorrect results.

return 0;

This ends the program successfully.