Convert Binary to Octal in C++
Convert binary to octal in C++ by going through decimal with two helper functions. Learn the three-bit grouping shortcut and the type limits of this approach.
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}
Sample Output
Binary: 1010 = Octal: 12
Via decimal 10.
In this one
- How to chain two conversions through decimal
- How helper functions keep main short
- The shortcut of grouping bits in threes
- Why decimalToOctal returning int limits the range
How it works
- 1
Read 1010.
- 2
binaryToDecimal gives 10.
- 3
decimalToOctal gives 12.
- 4
Print 12.
Edge cases
Large binary
Enter a 30-bit binary value.
Its octal form has up to 10 digits stored as a decimal number, which can exceed int when returned from decimalToOctal, giving a wrong value.
Invalid digits
Enter 1201.
The helper treats 2 as a bit weight times 2, producing a meaningless result. Validate input first.
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;
}
return decimal;
}
This function:
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;
}
return octal;
}
This function:
How it works (for decimal = 10):
Main Function - Combining Both Steps
int main() {
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;
}
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
#include <bitset>
bitset<32> binaryBits(binary);
string binaryStr = binaryBits.to_string();
// Group and convert
Method 4: Using String Manipulation
Method 5: Using Functions (Modular)
Displaying the Result
The program prints:
cout << "Binary: " << binary << " = Octal: " << octal << endl;
Output:
Binary: 1010 = Octal: 12
This clearly shows the conversion from binary to octal.
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
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.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Binary 111000 in octal?
Pick an answer to check it.
Question 2
Why does each octal digit match three bits?
Pick an answer to check it.
Questions people ask
What is the grouping shortcut?
Split the bits into groups of three from the right: 1 010 → 1 2 → 12.
Why go through decimal?
It reuses two simpler conversions you already know.
Can I use a library?
Read with stoi(s, nullptr, 2) and print with cout << oct.