C++ Program For Binary To Octal Conversion

Last Updated : 22 Aug, 2026

A binary number uses only 0 and 1, while an octal number uses digits from 0 to 7. This article explains how to convert a binary number, including large and fractional values, into its octal equivalent in C++.

  • Binary-to-octal conversion is convenient because three binary bits correspond to one octal digit.
  • String-based processing allows conversion of binary values that may be too large for built-in integer types.

Examples:  

Input: 110001110
Output: 616

Input: 1111001010010100001.010110110011011
Output: 1712241.26633 

Approaches to Convert Binary to Octal

The conversion can be performed using the following approaches:

1. Using Groups of Three Bits

Since one octal digit can represent values from 0 to 7, it can be represented using exactly three binary bits.

For example:

000 -> 0
001 -> 1
010 -> 2
011 -> 3
100 -> 4
101 -> 5
110 -> 6
111 -> 7

For the integer part, groups are formed from right to left. If the number of bits is not a multiple of 3, zeros are added to the left. For the fractional part, groups are formed from left to right. If required, zeros are added to the right.

Steps:

  1. Separate the integer and fractional parts using the decimal point.
  2. Add leading zeros to the integer part until its length is a multiple of 3.
  3. Add trailing zeros to the fractional part until its length is a multiple of 3.
  4. Process each group of three binary digits.
  5. Convert each group to its corresponding octal digit.
  6. Insert the decimal point between the two parts.
C++
#include <iostream>
#include <string>
using namespace std;

char binaryToOctalDigit(const string& bits)
{
    int value = 0;

    for (char bit : bits)
        value = value * 2 + (bit - '0');

    return char('0' + value);
}

string binaryToOctal(string binary)
{
    size_t dot = binary.find('.');

    string integerPart =
        (dot == string::npos) ? binary : binary.substr(0, dot);

    string fractionalPart =
        (dot == string::npos) ? "" : binary.substr(dot + 1);

    // Pad integer part on the left.
    while (integerPart.size() % 3 != 0)
        integerPart = "0" + integerPart;

    // Pad fractional part on the right.
    while (fractionalPart.size() % 3 != 0)
        fractionalPart += "0";

    string octal;

    // Convert integer part.
    for (size_t i = 0; i < integerPart.size(); i += 3)
        octal += binaryToOctalDigit(
            integerPart.substr(i, 3)
        );

    // Convert fractional part.
    if (!fractionalPart.empty()) {
        octal += '.';

        for (size_t i = 0; i < fractionalPart.size(); i += 3)
            octal += binaryToOctalDigit(
                fractionalPart.substr(i, 3)
            );
    }

    return octal;
}

int main()
{
    string binary =
        "1111001010010100001.010110110011011";

    cout << binaryToOctal(binary) << endl;

    return 0;
}

Output
1712241.26633

Explanation: The integer part is grouped from right to left:

1111001010010100001
-> 001 111 001 010 010 100 001
-> 1 7 1 2 2 4 1

The fractional part is grouped from left to right:

010110110011011
-> 010 110 110 011 011
-> 2 6 6 3 3

Combining both parts gives:

1712241.26633

This approach works directly on the input string, so the binary number does not need to fit into an integer or floating-point data type.

2. Using Decimal Conversion and Repeated Division

For binary numbers that fit within a built-in integer type, we can first convert the binary integer part to decimal and then convert that decimal value to octal. The integer conversion uses the binary positional representation, while the octal conversion repeatedly divides the decimal value by 8.

Steps:

  1. Validate the binary input.
  2. Separate the integer and fractional parts if a decimal point exists.
  3. Convert the integer part from binary to decimal.
  4. Repeatedly divide the decimal value by 8 to obtain octal digits.
  5. For a fractional part, repeatedly multiply its decimal value by 8 and take the integer part.
  6. Combine the integer and fractional octal parts.
C++
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

string binaryToOctal(const string& binary)
{
    size_t dot = binary.find('.');

    string integerPart =
        (dot == string::npos) ? binary : binary.substr(0, dot);

    // Convert binary integer to decimal.
    unsigned long long decimalValue = 0;

    for (char bit : integerPart) {
        if (bit != '0' && bit != '1')
            return "Invalid binary number";

        decimalValue = decimalValue * 2 + (bit - '0');
    }

    // Convert decimal integer to octal.
    string octal;

    if (decimalValue == 0) {
        octal = "0";
    } else {
        while (decimalValue > 0) {
            octal = char('0' + decimalValue % 8) + octal;
            decimalValue /= 8;
        }
    }

    // Convert fractional part if present.
    if (dot != string::npos) {
        string fractionalPart = binary.substr(dot + 1);

        octal += '.';

        double fraction = 0.0;
        double base = 0.5;

        for (char bit : fractionalPart) {
            if (bit != '0' && bit != '1')
                return "Invalid binary number";

            if (bit == '1')
                fraction += base;

            base /= 2.0;
        }

        // Generate up to 5 octal fractional digits.
        for (int i = 0; i < 5 && fraction > 0; i++) {
            fraction *= 8;

            int digit = static_cast<int>(fraction);
            octal += char('0' + digit);

            fraction -= digit;
        }
    }

    return octal;
}

int main()
{
    string binary = "110001110";

    cout << binaryToOctal(binary) << endl;

    return 0;
}

Output
616

Explanation: The binary number 110001110 is first converted to decimal:

110001110₂ = 398₁₀

The decimal value 398 is then converted to octal using repeated division by 8:

398 ÷ 8 = 49 remainder 6
49 ÷ 8 = 6 remainder 1
6 ÷ 8 = 0 remainder 6

Reading the remainders from bottom to top:

616

Therefore:

110001110₂ = 398₁₀ = 616₈

Note: This approach is suitable only when the binary integer part and its decimal conversion fit within the chosen data type. For very large binary values, the first approach is preferred because it processes the input directly as a string.

Comment