如何在Java中將十六進位制轉換為八進位制?


八進位制數 - 八進位制數也是一種可用的數字系統。八進位制數用 8 個數字表示,從 0 到 7(0、1、2、3...7)。八進位制數在數字系統中表示為以 8 為底。

十六進位制數 - 十六進位制數也是一種可用的數字系統。十六進位制數用 16 個數字表示,從 0 到 15(0、1、2、3...15)。從 10 到 15,它分別表示為 A 到 F。十六進位制數在數字系統中表示為以 16 為底。

在這裡,我們首先將十六進位制數轉換為二進位制數,其中每個數字的二進位制數組合為四位數。獲得所有這些二進位制數後,我們將所有這些數字連線起來,然後必須將整個二進位制數字集分成塊,其中每個部分包含三位數字。然後,我們可以將這些二進位制數字集轉換為八進位制數。透過這種方式,我們將十六進位制數轉換為八進位制數。

換句話說,在獲得十進位制值後,我們連續求 8 的模值,然後透過連線這些值,我們可以得到相應的八進位制值。

讓我們看看如何使用 Java 程式語言來實現這一點。

舉幾個例子

示例 1

輸入的十六進位制數為 9AD

其轉換後的十進位制值為 2477

現在 2477 的八進位制值為 4655

示例 2

輸入的十六進位制數為 219A

其轉換後的十進位制值為 8602

現在 8602 的八進位制值為 20632

示例 3

輸入的十六進位制數為 21AD45

其轉換後的十進位制值為 2207045

現在 2207045 的八進位制值為 10326505

演算法

步驟 1 - 將輸入數字作為字串型別獲取,可以透過靜態輸入方法或使用者輸入方法獲取。

步驟 2 - 使用一些 switch case 定義給定十六進位制數的每個數字的相應十進位制值。

步驟 3 - 獲取十進位制值後,透過連續求 8 的模值並連線它們來將其轉換為相應的八進位制值。

步驟 4 - 最後,將計算出的八進位制值作為輸出列印。

多種方法

我們提供了不同方法的解決方案。

  • 使用靜態輸入值

  • 透過使用者定義的方法

讓我們逐一檢視程式及其輸出。

方法 1:使用靜態輸入值

在這種方法中,我們透過靜態輸入方法宣告一個十六進位制輸入數字,並使用演算法將十六進位制數字轉換為八進位制數字。

示例

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 + ".");
   }
}

輸出

The Octal Value of 87FA is 103772.

方法 2:使用使用者定義的方法

在這種方法中,我們透過靜態輸入方法宣告一個十六進位制輸入數字,並將這些數字作為引數傳遞給使用者定義的方法,然後在方法內部使用演算法將十六進位制數字轉換為八進位制數字。

示例

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 + ".");
   }
}

輸出

The Octal Value of 6FE4 is 67744.

在本文中,我們探討了如何使用不同的方法在 Java 中將十六進位制數轉換為八進位制數。

更新於: 2023 年 3 月 6 日

877 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告