Monday, December 30, 2024
Google search engine
HomeLanguagesJavaJava Program to Convert Binary to Octal

Java Program to Convert Binary to Octal

A Binary Number System is composed of two symbols : 0’s (zeroes) and 1’s (ones), which represent low or Off and high or On state respectively in digital electronics. It is primarily a number system with the base-2 and is extensively used in computer science as well. All the data is stored in binary symbols in computers which are also called bits. The Binary System derives its name from the fact that it is composed of just two symbols. A Binary Number can also be thought of as a string of just 0’s and 1’s.

An Octal Number System comprises eight digits ranging from 0 to 7. It derives its name from the fact that it consists of eight digits (hence Oct) which means eight. It is an 8-base number system and can be formulated by grouping the bits in a binary number in groups of three and calculating the corresponding value of each group as a single digit in the resultant Octal Number.

For example Binary to Octal Conversion

Input : 100100
Output: 44
Input : 1100001
Output : 141

Methods for Binary to Octal Conversion

In this article, we are going to explore 2 methods to convert a Binary Number to an Octal Number. These methods are as follows :

  • Using the built-in toOctalString() method in Java
  • Converting the binary number to a decimal number which is further converted to the corresponding Octal number.

1. Using toOctalString() Method for Binary to Octal Conversion

Using this approach, we first convert a binary number to an Integer and then using the toOctalString() built-in method of Java convert it into a string of octal numbers. This string is then converted to an Integer again.

Syntax of toOctalString

public static String toOctalString(int num)

Parameters: The method accepts a single parameter num of integer type which is required to be converted to a string.

Return Value: The function returns a string representation of the integer argument as an unsigned integer in base 8.

Algorithm:

  1. Convert the binary number into a decimal number.
  2. Convert this decimal number to a string of octal numbers using the toOctalString() method.
  3. Convert this string again to an Integer.

Below is the implementation of the above method:

Java




// Java program to convert binary to octal
class GFG {
    // method to convert binary to decimal
    int binaryToDecimal(long binary)
    {
        // variable to store the decimal number
        int decimalNumber = 0, i = 0;
 
        // loop to extract the digits of the
        // binary number
        while (binary > 0) {
 
            // multiplying each digit of binary
            // with increasing powers of 2 towards
            // left
            decimalNumber
                += Math.pow(2, i++) * (binary % 10);
 
            // dividing the binary by 10
            // to remove the last digit
            binary /= 10;
        }
 
        // returning the converted decimal number
        return decimalNumber;
    }
 
    // function to convert decimal to octal
    int decimalToOctal(long binary)
    {
        // variable to store the decimal number
        // returned by the binaryToDecimal()
        int decimalNumber = binaryToDecimal(binary);
 
        // using the toOctalString() to convert
        // Integer to String of Octal Number
        String octalString
            = Integer.toOctalString(decimalNumber);
 
        // converting the String of octal number
        // to an Integer
        int octalNumber = Integer.parseInt(octalString);
 
        // returning the octal number
        return octalNumber;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // instantiating the class
        GFG ob = new GFG();
 
        // calling and printing the decimalToOCtal
        // method
        System.out.println("octal number:"
                           + ob.decimalToOctal(100100));
    }
}


Output

octal number:44

2. User Defined Function to Convert Binary to Octal

Using this approach, we first convert the binary number to a decimal number. We then convert this decimal number to an octal number by continuously extracting the remainder and dividing by 8.

Algorithm:

  1. Convert the binary number to a decimal number.
  2. Now, for this converted decimal number, run a while loop till this number becomes 0.
  3. In every iteration of the loop, get the remainder by dividing the number by 8.
  4. Multiply this remainder with increasing powers of 10.
  5. Finally divide the original number by 8.

Example:

Java




// Java program to convert binary to octal
 
// Driver Class
class GFG {
    // function to convert binary number
    // to decimal number
    int binaryToDecimal(long binary)
    {
        // variable to store the converted
        // decimal number
        int decimalNumber = 0, i = 0;
 
        // loop to convert binary to decimal
        while (binary > 0) {
 
            // extracting every digit of the
            // binary and multiplying with
            // increasing powers of 2
            decimalNumber
                += Math.pow(2, i++) * (binary % 10);
 
            // dividing the number by 10
            // to remove the last digit
            binary /= 10;
        }
 
        // returning the converted decimal
        // number
        return decimalNumber;
    }
 
    // function to convert decimal number
    // to octal
    int decimalToOctal(long binary)
    {
        // variable to store the octal number
        int octalNumber = 0, i = 0;
 
        // variable to store the output
        // returned by the binaryToDecimal()
        int decimalNumber = binaryToDecimal(binary);
 
        // loop to convert decimal to octal
        while (decimalNumber != 0) {
 
            // extracting the remainder on
            // multiplying by 8 and
            // dividing that with increasing
            // powers of 10
            octalNumber += (decimalNumber % 8)
                           * ((int)Math.pow(10, i++));
 
            // removing the last digit by
            // dividing by 8
            decimalNumber /= 8;
        }
 
        // returning the converted octal number
        return octalNumber;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // instantiating the class
        GFG ob = new GFG();
 
        // calling and printing the
        // decimalToOctal() function
        System.out.println(ob.decimalToOctal(1001001));
    }
}


Output

111

The complexity of the above method:

Time complexity: O(nlogn) as using pow function inside while loop
Auxiliary space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments