Program to convert a binary number to octal
Last Updated :
05 Dec, 2023
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++
#include <bits/stdc++.h>
using namespace std;
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' ;
}
string convertBinToOct(string bin)
{
int l = bin.size();
int t = bin.find_first_of( '.' );
int len_left = t != -1 ? t : l;
for ( int i = 1; i <= (3 - len_left % 3) % 3; i++)
bin = '0' + bin;
if (t != -1)
{
int len_right = l - len_left - 1;
for ( int i = 1; i <= (3 - len_right % 3) % 3; i++)
bin = bin + '0' ;
}
unordered_map<string, char > bin_oct_map;
createMap(&bin_oct_map);
int i = 0;
string octal = "" ;
while (1)
{
octal += bin_oct_map[bin.substr(i, 3)];
i += 3;
if (i == bin.size())
break ;
if (bin.at(i) == '.' )
{
octal += '.' ;
i++;
}
}
return octal;
}
int main()
{
string bin = "1111001010010100001.010110110011011" ;
cout << "Octal number = "
<< convertBinToOct(bin);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void createMap(Map<String, Character> um)
{
um.put( "000" , '0' );
um.put( "001" , '1' );
um.put( "010" , '2' );
um.put( "011" , '3' );
um.put( "100" , '4' );
um.put( "101" , '5' );
um.put( "110" , '6' );
um.put( "111" , '7' );
}
static String convertBinToOct(String bin)
{
int l = bin.length();
int t = bin.indexOf( '.' );
int len_left = t != - 1 ? t : l;
for ( int i = 1 ;
i <= ( 3 - len_left % 3 ) % 3 ;
i++)
bin = '0' + bin;
if (t != - 1 )
{
int len_right = l - len_left - 1 ;
for ( int i = 1 ;
i <= ( 3 - len_right % 3 ) % 3 ;
i++)
bin = bin + '0' ;
}
Map<String,
Character> bin_oct_map = new HashMap<String,
Character>();
createMap(bin_oct_map);
int i = 0 ;
String octal = "" ;
while ( true )
{
octal += bin_oct_map.get(
bin.substring(i, i + 3 ));
i += 3 ;
if (i == bin.length())
break ;
if (bin.charAt(i) == '.' )
{
octal += '.' ;
i++;
}
}
return octal;
}
public static void main(String[] args)
{
String bin = "1111001010010100001.010110110011011" ;
System.out.println( "Octal number = " +
convertBinToOct(bin));
}
}
|
Python3
def createMap(bin_oct_map):
bin_oct_map[ "000" ] = '0'
bin_oct_map[ "001" ] = '1'
bin_oct_map[ "010" ] = '2'
bin_oct_map[ "011" ] = '3'
bin_oct_map[ "100" ] = '4'
bin_oct_map[ "101" ] = '5'
bin_oct_map[ "110" ] = '6'
bin_oct_map[ "111" ] = '7'
def convertBinToOct( bin ):
l = len ( bin )
t = - 1
if '.' in bin :
t = bin .index( '.' )
len_left = t
else :
len_left = l
for i in range ( 1 , ( 3 - len_left % 3 ) % 3 + 1 ):
bin = '0' + bin
if (t ! = - 1 ):
len_right = l - len_left - 1
for i in range ( 1 , ( 3 - len_right % 3 ) % 3 + 1 ):
bin = bin + '0'
bin_oct_map = {}
createMap(bin_oct_map)
i = 0
octal = ""
while ( True ) :
octal + = bin_oct_map[ bin [i:i + 3 ]]
i + = 3
if (i = = len ( bin )):
break
if ( bin [i] = = '.' ):
octal + = '.'
i + = 1
return octal
bin = "1111001010010100001.010110110011011"
print ( "Octal number = " ,
convertBinToOct( bin ))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void createMap(Dictionary<String, char > um)
{
um.Add( "000" , '0' );
um.Add( "001" , '1' );
um.Add( "010" , '2' );
um.Add( "011" , '3' );
um.Add( "100" , '4' );
um.Add( "101" , '5' );
um.Add( "110" , '6' );
um.Add( "111" , '7' );
}
static String convertBinToOct(String bin)
{
int l = bin.Length;
int t = bin.IndexOf( '.' );
int i = 0;
int len_left = t != -1 ? t : l;
for (i = 1;
i <= (3 - len_left % 3) % 3;
i++)
bin = '0' + bin;
if (t != -1)
{
int len_right = l - len_left - 1;
for (i = 1;
i <= (3 - len_right % 3) % 3;
i++)
bin = bin + '0' ;
}
Dictionary<String,
char > bin_oct_map = new Dictionary<String,
char >();
createMap(bin_oct_map);
i = 0;
String octal = "" ;
while ( true )
{
octal += bin_oct_map[
bin.Substring(i, 3)];
i += 3;
if (i == bin.Length)
break ;
if (bin[i] == '.' )
{
octal += '.' ;
i++;
}
}
return octal;
}
public static void Main(String[] args)
{
String bin = "1111001010010100001.010110110011011" ;
Console.WriteLine( "Octal number = " +
convertBinToOct(bin));
}
}
|
Javascript
<script>
function createMap(um)
{
um.set( "000" , '0' );
um.set( "001" , '1' );
um.set( "010" , '2' );
um.set( "011" , '3' );
um.set( "100" , '4' );
um.set( "101" , '5' );
um.set( "110" , '6' );
um.set( "111" , '7' );
}
function convertBinToOct(bin)
{
let l = bin.length;
let t = bin.indexOf( '.' );
let len_left = t != -1 ? t : l;
for (let i = 1;
i <= (3 - len_left % 3) % 3;
i++)
bin = '0 ' + bin;
// If decimal point exists
if (t != -1)
{
// Length of string after ' . '
let len_right = l - len_left - 1;
// add min 0' s in the end to make right
for (let i = 1;
i <= (3 - len_right % 3) % 3;
i++)
bin = bin + '0' ;
}
let bin_oct_map = new Map();
createMap(bin_oct_map);
let i = 0;
let octal = "" ;
while ( true )
{
octal += bin_oct_map.get(bin.substr(i, 3));
i += 3;
if (i == bin.length)
break ;
if (bin.charAt(i) == '.' )
{
octal += '.' ;
i++;
}
}
return octal;
}
let bin = "1111001010010100001.010110110011011" ;
document.write( "Octal number = " +
convertBinToOct(bin));
</script>
|
Output
Octal number = 1712241.26633
Time Complexity: O(n), where n is the length of string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...