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.
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}
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
Read 12.
- 2
octalToDecimal gives 10.
- 3
decimalToBinary gives 1010.
- 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:
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;
}
return decimal;
}
This function:
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;
}
return binary;
}
This function:
How it works (for decimal = 10):
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:
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
#include <bitset>
// Convert each octal digit to binary using bitset
// Concatenate results
Method 4: Using String Manipulation
Method 5: Using Functions (Modular)
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?:
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
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.
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)).