Convert Binary to Octal in C++
Convert Binary to Octal in C++ (5 Programs).
1#include <iostream>2#include <cmath>3using namespace std;45int binaryToDecimal(long long binary) {6 int decimal = 0, i = 0, remainder;7 while (binary != 0) {8 remainder = binary % 10;9 binary /= 10;10 decimal += remainder * pow(2, i);11 ++i;12 }13 return decimal;14}1516int decimalToOctal(int decimal) {17 long long octal = 0;18 int remainder, i = 1;19 while (decimal != 0) {20 remainder = decimal % 8;21 decimal /= 8;22 octal += remainder * i;23 i *= 10;24 }25 return octal;26}2728int main() {29 long long binary;30 cout << "Enter a binary number: ";31 cin >> binary;3233 int decimal = binaryToDecimal(binary);34 long long octal = decimalToOctal(decimal);3536 cout << "Binary: " << binary << " = Octal: " << octal << endl;3738 return 0;39}
Output
Enter a binary number: 1010 Binary: 1010 = Octal: 12
What's going on
The program converts a binary number to an octal number. For example:
The program uses a two-step approach: first converts binary to decimal, then decimal to octal. 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: Binary to Decimal
Step 2: Decimal to Octal
Why Two Steps?
:
Function 1: binaryToDecimal()
int binaryToDecimal(long long binary) {
int decimal = 0, i = 0, remainder;
while (binary != 0) {
remainder = binary % 10;
binary /= 10;
decimal += remainder * pow(2, i);
++i;
}
}
This function:
1return decimal;
How it works
(for binary = 1010):
Function 2: decimalToOctal()
int decimalToOctal(int decimal) {
long long octal = 0;
int remainder, i = 1;
while (decimal != 0) {
remainder = decimal % 8;
decimal /= 8;
octal += remainder * i;
i *= 10;
}
}
This function:
1return octal;
How it works
(for decimal = 10):
Main Function - Combining Both Steps
long long binary;
cout << "Enter a binary number: ";
cin >> binary;
int decimal = binaryToDecimal(binary);
long long octal = decimalToOctal(decimal);
cout << "Binary: " << binary << " = Octal: " << octal << endl;
return 0;
}
1int main() {
Process Flow
:
Direct Conversion Method (More Efficient)
Why Direct Conversion?
:
How Direct Conversion Works
:
Example
(binary 1010 to octal):
Why Groups of 3?
:
Other Methods (Mentioned but not shown in code)
Method 2: Direct Conversion
Method 3: Using bitset
bitset<32> binaryBits(binary);
string binaryStr = binaryBits.to_string();
// Group and convert
1#include <bitset>
Method 4: Using String Manipulation
Method 5: Using Functions (Modular)
Displaying the Result
The program prints:
Output:
Binary: 1010 = Octal: 12
This clearly shows the conversion from binary to octal.
1cout << "Binary: " << binary << " = Octal: " << octal << endl;
Understanding Binary-Octal Relationship
Why 3 Binary Digits = 1 Octal Digit?
:
Conversion Table
(3-bit binary to octal):
Direct Conversion Example
:
Binary: 1 0 1 0
Group: 1 | 010
001 | 010 (pad left group with 0)
Octal: 1 | 2
Result: 12 (octal)
When to Use Each Method
-
Two-Step Conversion
: Best for learning - clear, reuses known algorithms.
-
Direct Conversion
: Best for efficiency - faster, groups 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
Padding with Zeros
:
Large Binary Numbers
:
Validation
:
return 0;
This ends the program successfully.