Home  >  Article  >  Java  >  Convert hexadecimal to octal in Java?

Convert hexadecimal to octal in Java?

WBOY
WBOYforward
2023-08-19 13:37:051266browse

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.

让我们看看如何使用Java编程语言来完成这个任务。

To show you some instances

Instance-1

的中文翻译为:

实例-1

Input Hexadecimal number is 9AD

The decimal converted value of it = 2477

Now the octal value of 2477 = 4655

Instance-2

的中文翻译为:

实例-2

Input Hexadecimal number is 219A

The decimal converted value of it = 8602

现在8602的八进制值为20632

Instance-3

的中文翻译为:

实例-3

Input Hexadecimal number is 21AD45

它的十进制转换值 = 2207045

现在2207045的八进制值为10326505

算法

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.

第三步 - 在获得十进制值后,我们通过不断地找到8的模数值并连接它们来将其转换为适当的八进制值。

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

Multiple Approaches

We have provided the solution in different approaches.

  • 通过使用静态输入值

  • 通过用户定义的方法

让我们逐个查看程序及其输出。

途径-1:通过使用静态输入值

在这种方法中,我们通过静态输入方法声明一个十六进制输入数字,通过使用算法,我们可以将十六进制数字转换为八进制数字。

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.

The above is the detailed content of Convert hexadecimal to octal in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete