Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Convert Octal to Binary in C++

Convert octal to binary in C++ by converting to decimal first and then to binary with two helpers. Includes the digit-to-three-bits shortcut for checking answers.

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}

Sample Output

Octal: 12 = Binary: 1010

Via decimal 10.

In this one

  • How to compose two helper functions
  • How each octal digit expands to three bits
  • How long long stores binary digits
  • What breaks with digits 8 or 9

How it works

  1. 1

    Read 12.

  2. 2

    octalToDecimal gives 10.

  3. 3

    decimalToBinary gives 1010.

  4. 4

    Print it.

Edge cases

Large octal

Enter 7777777.

The binary form has 21 digits, which no longer fits in long long and i also overflows int.

Invalid digit 9

Enter 19.

It is computed as 1×8 + 9 = 17 → 10001, but 19 is not octal at all.

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;

}

return decimal;

}

This function:

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

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;

}

return binary;

}

This function:

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

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

int main() {

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;

}

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

#include <bitset>

// Convert each octal digit to binary using bitset

// Concatenate results

Uses bitset for binary representation.
Converts each digit and combines.

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:

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

Output:

Octal: 12 = Binary: 1010

This clearly shows the conversion from octal to binary.

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.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Octal 7 in binary?

Pick an answer to check it.

Question 2

Octal 10 in binary?

Pick an answer to check it.

Questions people ask

Shortcut?

Replace each octal digit with three bits: 1 → 001, 2 → 010, so 12 → 001010 → 1010.

Why does the output drop leading zeros?

It is stored as a number, which cannot hold leading zeros.

Library option?

bitset<32>(stoi(s, nullptr, 8)).