Convert Hexadecimal to Octal in Java?


Octal Number − Octal number is also one of the number systems available. The octal number is represented with 8 digits which is from 0 to 7(0, 1, 2, 3... 7). The Octal numbers are expressed as base-8 in the numeral system.

Hexadecimal Number − Hexadecimal number is also one of the number systems available. The Hexadecimal number is represented with 16 digits which is from 0 to 15(0, 1, 2, 3... 15). From 10 to 15 it is represented as A to F. The Hexadecimal numbers are expressed as base-16 in the numeral system.

Here we first convert the hexadecimal numbers into binary numbers, where we get the binary numbers combination of four digits for each digit. After getting all those binary digits we concatenate all those digits then we have to divide the whole set of binary numbers into chucks where every part consists of three digits. Then we can convert those sets of binary numbers into octal numbers. By this way we convert the Hexadecimal number into octal number.

In other way after getting the decimal value we continuously find the modulus by 8 values and then by concatenating those values we can get the appropriate octal value.

Let’s see how this can be done by using Java programming language.

To show you some instances

Instance-1

Input Hexadecimal number is 9AD

The decimal converted value of it = 2477

Now the octal value of 2477 = 4655

Instance-2

Input Hexadecimal number is 219A

The decimal converted value of it = 8602

Now the octal value of 8602= 20632

Instance-3

Input Hexadecimal number is 21AD45

The decimal converted value of it = 2207045

Now the octal value of 2207045= 10326505

Algorithm

Step 1 − Get the input number as string type either by static input method or user input method.

Step 2 − Using some switch cases we define the appropriate decimal value of each digit of the given hexadecimal number.

Step 3 − After getting the decimal value we convert it to the appropriate octal value by continuously finding the modulus by 8 values and concatenating those.

Step 4 − At the end we print the calculated Octal value as output.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value

  • By User-Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, we declare a hexadecimal input number by static input method and by using the algorithm we can convert the hexadecimal number into octal number.

Example

public class Main{
   public static void main(String[] args){
      
      //declare a variable to store the decimal number
      int decimalNumber = 0;
      
      //Declare and store a hexadecimal number by static input method.
      String hexadecimalNumber = "87FA";
      int a = hexadecimalNumber.length() - 1;
      
      //Loop to find the appropriate decimal number of given hexadecimal number
      for(int i = 0; i < hexadecimalNumber.length() ; i ++ ){
         
         //extract the character from the string.
         char c = hexadecimalNumber.charAt(i);
         switch (c){
            case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8':
            case '9':
               decimalNumber = decimalNumber + Integer.parseInt(Character.toString(c))*(int)Math.pow(16,a);
               a--; break;
            case 'a': case 'A':
               decimalNumber = decimalNumber + 10 * (int)Math.pow(16, a);
               a--; break;
            case 'b': case 'B':
               decimalNumber = decimalNumber + 11 * (int)Math.pow(16, a);
               a--; break;
            case 'c': case 'C':
               decimalNumber = decimalNumber + 12 * (int)Math.pow(16, a);
               a--; break;
            case 'd': case 'D':
               decimalNumber = decimalNumber + 13 * (int)Math.pow(16, a);
               a--;
               break;
            case 'e': case 'E':
               decimalNumber = decimalNumber + 14 * (int)Math.pow(16, a);
                a--; break;
            case 'f': case 'F':
               decimalNumber = decimalNumber + 15 * (int)Math.pow(16, a);
               a--; break;
            default:
            System.out.println("The number you have entered is invalid."); break;
         }
      }
      String octalNumber ="";// declare a variable to store the octal number in string format.
      
      //initiate the loop to convert decimal number into octal number.
      while(decimalNumber > 0){
         octalNumber = decimalNumber % 8 + octalNumber;
         decimalNumber = decimalNumber / 8;
      }
      System.out.println("The Octal Value of "+ hexadecimalNumber + " is " + octalNumber + ".");
   }
}

Output

The Octal Value of 87FA is 103772.

Approach-2: By Using User Defined Method

In this approach, we declare a hexadecimal input number by static input method and pass these numbers as parameters in a user defined method, then inside the method by using the algorithm we can convert the hexadecimal number into octal number.

Example

public class Main{
   public static void main(String[] args){
      String inputNumber = "6FE4";//Declare and store a hexadecimal number by static input method.
      hexToOct(inputNumber);//call the user defined method to convert given hexadecimal number into octal.
   }
   //user defined method to convert the hexadecimal number into octal number
   public static void hexToOct(String hexadecimalNumber){
      int decimalNumber = 0;//declare a variable to store the decimal number
      int a = hexadecimalNumber.length() - 1;
      //Loop to find the appropriate decimal number of given hexadecimal number
      for(int i = 0; i < hexadecimalNumber.length() ; i ++ ){
         //extract the character from the string.
         char c = hexadecimalNumber.charAt(i);
         switch (c){
            case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8':
            case '9':
               decimalNumber = decimalNumber + Integer.parseInt(Character.toString(c))*(int)Math.pow(16,a);
               a--; break;
            case 'a': case 'A':
               decimalNumber = decimalNumber + 10 * (int)Math.pow(16, a);
               a--; break;
            case 'b': case 'B':
               decimalNumber = decimalNumber + 11 * (int)Math.pow(16, a);
               a--; break;
            case 'c':
               decimalNumber = decimalNumber + 12 * (int)Math.pow(16, a);
               a--; break;
            case 'd': case 'D':
               decimalNumber = decimalNumber + 13 * (int)Math.pow(16, a);
               a--; break;
            case 'e': case 'E':
               decimalNumber = decimalNumber + 14 * (int)Math.pow(16, a);
               a--; break;
            case 'f': case 'F':
               decimalNumber = decimalNumber + 15 * (int)Math.pow(16, a);
               a--; break;
            default:
               System.out.println("The number you have entered is invalid."); break;
         }
      }
      String octalNumber ="";// declare a variable to store the octal number in string format.
      //initiate the loop to convert decimal number into octal number.
      while(decimalNumber > 0){
         octalNumber = decimalNumber % 8 + octalNumber;
         decimalNumber = decimalNumber / 8;
      }
      System.out.println("The Octal Value of "+ hexadecimalNumber + " is " + octalNumber + ".");
   }
}

Output

The Octal Value of 6FE4 is 67744.

In this article, we explored how to convert a hexadecimal number to octal number in Java by using different approaches.

Updated on: 06-Mar-2023

704 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements