Open In App

Converting Binary To Octal number using HashMap in Java

Last Updated : 02 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Remember while we were converting octal to decimal we were taking 3 binary digits at a time. A similar approach will be used where here for every 3 digits we are having a corresponding number as in octal system we have numbers from 0 to ‘R-1’ where R represents base value of number system. As the name suggests, in an octal number system, R is equivalent to 8. Hence, the number is as follows: 0,1,2,3,4,5,6,7. 

Now going by virtue of HashMaps in converting one thing is clear from here that one way or other binary equivalent and octal equivalent are going to act as key-value pairs in our HashMap.

Illustration:

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

Algorithm:

  1. Convert the binary number into a decimal number.
  2. Using HashMap we map every bit with its decimal values.
  3. When the bit is found it maps with the decimal number
  4. Prints and display the octal equivalent of the number.

Example:

Input  1 : 1011
Output 1 : 13

Input 2 : 1000
Output 2 : 10

Implementation:

Java




// Java Program to Convert Binary Number to Octal Number
 
// Importing all utility classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Method 1
    // Helper method
    public static void octal(String s)
    {
 
        // Here binary number is represented by string 's'
        // over which standard length() method is computed
        // to get the length of binary number
 
        // Appending 2 zeros if binary numbers leaves
        // remainder as 1 after dividing with 3
        if (s.length() % 3 == 1) {
 
            // Append  two zeros to it
            s = "00" + s;
        }
 
        // If binary string number length after equals 2
        else if (s.length() % 3 == 2) {
 
            // Concatenate string by adding 1 zero to it
            s = "0" + s;
        }
 
        // Creating an object of HashMap
        // Declaring object of String and Integer types
        HashMap<String, Integer> hm = new HashMap<>();
 
        // Adding elements to the object created above
        // using the put() method
 
        // Adding elements(key-value) pairs to given object
        // 000 in binary system  ->  0 in octal system
        // 001 in binary system  ->  1 in octal system
 
        // Similarly adding for 0 to N-1 (N=8 for octal)
        hm.put("000", 0);
        hm.put("001", 1);
        hm.put("010", 2);
        hm.put("011", 3);
        hm.put("100", 4);
        hm.put("101", 5);
        hm.put("110", 6);
        hm.put("111", 7);
 
        // Creating and declaring a string array
        String[] part = new String[3];
        int t = 0;
 
        // Iterating over the binary number digits
        for (int i = 0; i < s.length(); i = i + 3) {
 
            // Checking for substring in an binary number
            // digit array
            String bypart = s.substring(i, i + 3);
            part[t] = bypart;
 
            // If found
            if (hm.containsKey(part[t])) {
 
                // Getting the part to be matched for
                // its corresponding octal numbers
                System.out.print(hm.get(part[t]));
            }
 
            // Incrementing the counter
            t++;
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Display message
        System.out.print("Enter the binary number to be converted : ");
        // Binary number to be converted
        // Custom entry 
        String s = 011;
         
        // Calling the method1 octal() over the
        // above input entered number
        octal(s);
       
        // Display message
        System.out.print("Octal equivalent : ");
    }
}


Output: 

Enter the binary number to be converted : 011
Octal equivalent : 3

Time Complexity: O(N)

Auxiliary Space: O(N)
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads