C++ Program For Binary To Octal Conversion
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
The idea is to consider the binary input as a string of characters and then follow the steps:
- Get length of substring to the left and right of the decimal point(‘.’) as left_len and right_len.
- 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.
- 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.
- Now, from the left extract one by one substrings of length 3 and add its corresponding octal code to the result.
- If in between a decimal(‘.’) is encountered then add it to the result.
C++
// 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 code 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.
Auxiliary space: O(1).
Method 2
The steps for this approach are as follows:
- Convert the given binary number into groups of three digits starting from the rightmost side.
- Convert each group of three digits to its corresponding octal digit.
- Concatenate the octal digits obtained in step 2 to get the octal equivalent of the given binary number.
C++
#include <iostream> #include <string> #include <cmath> using namespace std; string binaryToOctal(string binary) { // Check if the binary number is valid if (binary.find_first_not_of( "01." ) != string::npos) { return "Invalid binary number" ; } // Convert the integer part of the binary number to octal int decimal = stoi(binary.substr(0, binary.find( '.' )), nullptr, 2); string octal = "" ; while (decimal > 0) { octal = to_string(decimal % 8) + octal; decimal /= 8; } // Convert the fractional part of the binary number to octal if (binary.find( '.' ) != string::npos) { double fractional = stod( "0." + binary.substr(binary.find( '.' ) + 1)); octal += "." ; for ( int i = 0; i < 5; i++) { fractional *= 8; octal += to_string(( int ) floor (fractional)); fractional -= floor (fractional); } } return octal; } int main() { string binary1 = "110001110" ; string octal1 = binaryToOctal(binary1); cout << "Octal equivalent of " << binary1 << " is " << octal1 << endl; string binary2 = "1111001010010100001" ; string octal2 = binaryToOctal(binary2); cout << "Octal equivalent of " << binary2 << " is " << octal2 << endl; string binary3 = "11011.10" ; string octal3 = binaryToOctal(binary3); cout << "Octal equivalent of " << binary3 << " is " << octal3 << endl; string binary4 = "1002" ; string octal4 = binaryToOctal(binary4); cout << "Octal equivalent of " << binary4 << " is " << octal4 << endl; return 0; } |
Output
Octal equivalent of 110001110 is 616 Octal equivalent of 1111001010010100001 is 1712241 Octal equivalent of 11011.10 is 33.06314 Octal equivalent of 1002 is Invalid binary number
The time complexity of this approach is O(n), where n is the number of digits in the given binary number.
The auxiliary space required is O(n/3), since we need to store the octal digits obtained for each group of three binary digits.
Please Login to comment...