Convert Octal to Binary in C++
Convert Octal to Binary in C++ (5 Programs).
1#include <iostream>2#include <cmath>3using namespace std;45int 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}1516long 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}2728int main() {29 long long octal;30 cout << "Enter an octal number: ";31 cin >> octal;3233 int decimal = octalToDecimal(octal);34 long long binary = decimalToBinary(decimal);3536 cout << "Octal: " << octal << " = Binary: " << binary << endl;3738 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:
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:
Header Files Used
Understanding the Two-Step Conversion
The program uses two helper functions:
Step 1: Octal to Decimal
Step 2: Decimal to Binary
Why Two Steps?
:
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:
1return decimal;
How it works
(for octal = 12):
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:
1return binary;
How it works
(for decimal = 10):
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
:
Direct Conversion Method (More Efficient)
Why Direct Conversion?
:
How Direct Conversion Works
:
Example
(octal 12 to binary):
Why 1 Octal Digit = 3 Binary Digits?
:
Other Methods (Mentioned but not shown in code)
Method 2: Direct Conversion
Method 3: Using bitset
1#include <bitset>2// Convert each octal digit to binary using bitset3// Concatenate results
Method 4: Using String Manipulation
Method 5: Using Functions (Modular)
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?
:
Conversion Table
(octal to 3-bit 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
:
Large Octal Numbers
:
Validation
:
return 0;
This ends the program successfully.