Convert a binary number to octal | Java implementation to convert a binary number to octal number ; Function to create map between binary number and its equivalent hexadecimal ; Function to find octal equivalent of binary ; Length of string before ' . ' ; Add min 0 's in the beginning to make  left substring length divisible by 3 ; If decimal point exists ; Length of string after ' . ' ; add min 0 's in the end to make right  substring length divisible by 3 ; Create map between binary and its equivalent octal code ; One by one extract from left , substring of size 3 and add its octal code ; If ' . ' is encountered add it to result ; Required octal number ; Driver code
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 ) ) ; } }