Convert a binary number to octal

The problem is to convert the given binary number (represented as string) to its equivalent octal number. The input could be very large and may not fit even into unsigned long long int.

Examples:

```Input : 110001110
Output : 616

Input  : 1111001010010100001.010110110011011
Output : 1712241.26633
```

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to consider the binary input as a string of characters and then follow the steps:

1. Get length of substring to the left and right of the decimal point(‘.’) as left_len and right_len.
2. If left_len is not a multiple of 3 add min number of 0’s in the beginning to make length of left substring a multiple of 3.
3. If right_len is not a multiple of 3 add min number of 0’s in the end to make length of right substring a multiple of 3.
4. Now, from the left extract one by one substrings of length 3 and add its corresponding octal code to the result.
5. If in between a decimal(‘.’) is encountered then add it to the result.
```// C++ implementation to convert a binary number
// to octal number
#include <bits/stdc++.h>
using namespace std;

// function to create map between binary
// number and its equivalent octal
void createMap(unordered_map<string, char> *um)
{
(*um)["000"] = '0';
(*um)["001"] = '1';
(*um)["010"] = '2';
(*um)["011"] = '3';
(*um)["100"] = '4';
(*um)["101"] = '5';
(*um)["110"] = '6';
(*um)["111"] = '7';
}

// Function to find octal equivalent of binary
string convertBinToOct(string bin)
{
int l = bin.size();
int t = bin.find_first_of('.');

// length of string before '.'
int len_left = t != -1 ? t : l;

// add min 0's in the beginning to make
// left substring length divisible by 3
for (int i = 1; i <= (3 - len_left % 3) % 3; i++)
bin = '0' + bin;

// if decimal point exists
if (t != -1)
{
// length of string after '.'
int len_right = l - len_left - 1;

// add min 0's in the end to make right
// substring length divisible by 3
for (int i = 1; i <= (3 - len_right % 3) % 3; i++)
bin = bin + '0';
}

// create map between binary and its
// equivalent octal code
unordered_map<string, char> bin_oct_map;
createMap(&bin_oct_map);

int i = 0;
string octal = "";

while (1)
{
// one by one extract from left, substring
// of size 3 and add its octal code
octal += bin_oct_map[bin.substr(i, 3)];
i += 3;
if (i == bin.size())
break;

// if '.' is encountered add it to result
if (bin.at(i) == '.')
{
octal += '.';
i++;
}
}

// required octal number
return octal;
}

// Driver program to test above
int main()
{
string bin = "1111001010010100001.010110110011011";
cout << "Octal number = "
<< convertBinToOct(bin);
return 0;
}
```

Output:

```Octal number = 1712241.26633
```

Time Complexity: O(n), where n is the length of string.

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1 Average Difficulty : 1/5.0
Based on 1 vote(s)