Convert Binary to Octal in C++

Convert Binary to Octal in C++ (5 Programs).

IntermediateAdvanced Number ProgramsExample 9 of 13
convert-binary-to-octal.cpp
1#include <iostream>
2#include <cmath>
3using namespace std;
4
5int 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}
15
16int 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}
27
28int main() {
29 long long binary;
30 cout << "Enter a binary number: ";
31 cin >> binary;
32
33 int decimal = binaryToDecimal(binary);
34 long long octal = decimalToOctal(decimal);
35
36 cout << "Binary: " << binary << " = Octal: " << octal << endl;
37
38 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:

Input binary: 1010
Output octal: 12

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:

1010 (binary) → 10 (decimal) → 12 (octal)
1111 (binary) → 15 (decimal) → 17 (octal)

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 binary-to-decimal conversion.

Understanding the Two-Step Conversion

The program uses two helper functions:

Step 1: Binary to Decimal

Converts the binary number to decimal first.
Uses the algorithm: multiply each digit by 2^position and sum.

Step 2: Decimal to Octal

Converts the decimal result to octal.
Uses the algorithm: repeatedly divide by 8 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: 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:

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

How it works

(for binary = 1010):

Extracts digits from right to left.
Multiplies each digit by 2^position.
Sums all contributions: 0×1 + 1×2 + 0×4 + 1×8 = 10

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:

Takes a decimal number as input.
Converts it to octal using the standard algorithm.
Returns the octal equivalent.
1return octal;

How it works

(for decimal = 10):

Repeatedly divides by 8.
Collects remainders: 10÷8=1 rem 2, 1÷8=0 rem 1.
Builds octal: 2 + 1×10 = 12

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

:

1.User enters binary number (e.g., 1010).
2.Convert binary to decimal (1010 → 10).
3.Convert decimal to octal (10 → 12).
4.Display the result.

Direct Conversion Method (More Efficient)

Why Direct Conversion?

:

Two-step method works but is less efficient.
Direct conversion groups binary digits and converts directly.
More efficient: O(log n) vs O(log n) + O(log n).

How Direct Conversion Works

:

Group binary digits in sets of 3 (from right to left).
Each group of 3 binary digits = 1 octal digit.
Convert each group directly to octal.

Example

(binary 1010 to octal):

Group from right: 010 (pad with 0 if needed)
010 (binary) = 2 (octal)
Next group: 1 (pad to 001)
001 (binary) = 1 (octal)
Result: 12 (octal)

Why Groups of 3?

:

2³ = 8, so 3 binary digits can represent 0-7 (all octal digits).
This is the mathematical relationship between binary and octal.

Other Methods (Mentioned but not shown in code)

Method 2: Direct Conversion

Groups binary digits in sets of 3.
Converts each group directly to octal digit.
More efficient than two-step method.

Method 3: Using bitset

bitset<32> binaryBits(binary);

string binaryStr = binaryBits.to_string();

// Group and convert

Uses bitset for binary representation.
Groups and converts to octal.
1#include <bitset>

Method 4: Using String Manipulation

Converts binary to string.
Groups characters in sets of 3.
Converts each group to octal digit.

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:

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?

:

Binary is base-2, octal is base-8.
2³ = 8, so 3 binary digits can represent 0-7 (all octal digits).
This makes direct conversion possible.

Conversion Table

(3-bit binary to octal):

000 (binary) = 0 (octal)
001 (binary) = 1 (octal)
010 (binary) = 2 (octal)
011 (binary) = 3 (octal)
100 (binary) = 4 (octal)
101 (binary) = 5 (octal)
110 (binary) = 6 (octal)
111 (binary) = 7 (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

:

When grouping binary digits, leftmost group may need padding.
Pad with leading zeros to make groups of 3.
Example: 1010 → group as 001 and 010.

Large Binary Numbers

:

Very large binary 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 binary input contains only 0s and 1s.
This program assumes valid binary input.

return 0;

This ends the program successfully.