Java - Character forDigit() 方法



Java 的 Character forDigit() 方法用於確定指定基數中特定數字的字元表示。

基數定義為數字系統中唯一數字(包括零)的數量。最常見的基數是 10,表示十進位制系統 (0-9)。

使用此方法的優勢在於,它允許您生成並非真正“數字”的數字。例如,在十六進位制數字系統中,'9' 之後的數字使用字元 'A' 到 'F' 表示。

語法

以下是 Java Character forDigit() 方法的語法

public static char forDigit(int digit, int radix)

引數

  • digit − 要轉換為字元的數字。如果 0 ≤ digit < radix,則此引數有效。

  • radix − 基數。如果 MIN_RADIX ≤ radix ≤ MAX_RADIX,則此引數有效。

返回值

此方法返回指定基數中指定數字的 char 表示。一些特殊情況包括:

  • 如果 radix 的值不是有效的基數,或者 digit 的值不是指定基數中有效的數字,則返回空字元 ('\u0000')。

  • 如果 digit 小於 10,則返回 '0' + digit。否則,返回 'a' + digit − 10。

獲取特定數字和給定基數的字元表示示例

以下示例顯示了 Java Character forDigit() 方法的使用方法。我們建立了兩個 int 變數並用 int 值初始化,然後使用 forDigit() 方法,我們檢索了 10 和 16 基數的 char 表示,並列印了結果。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 character primitives ch1, ch2
      char ch1, ch2;

      // create 2 int primitives i1, i2 and assign values
      int i1 = 3;
      int i2 = 14;

      // assign char representation of i1, i2 to ch1, ch2 using radix
      ch1 = Character.forDigit(i1, 10);
      ch2 = Character.forDigit(i2, 16);
      String str1 = "Char representation of " + i1 + " in radix 10 is " + ch1;
      String str2 = "Char representation of " + i2 + " in radix 16 is " + ch2;

      // print ch1, ch2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Char representation of 3 in radix 10 is 3
Char representation of 14 in radix 16 is e

獲取超出數字和給定基數的字元表示示例

在此示例中,如果傳遞給方法的引數超過 MAX_RADIX,則返回值將為空字元。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 character primitives ch1, ch2
      char ch1, ch2;

      // create 2 int primitives i1, i2 and assign values
      int i1 = 15;
      int i2 = 20;

      // assign char representation of i1, i2 to ch1, ch2 using radix
      ch1 = Character.forDigit(i1, 10);
      ch2 = Character.forDigit(i2, 16);

      // print ch1, ch2 values
      System.out.println("Char representation of " + i1 + " in radix 10 is " + ch1);
      System.out.println("Char representation of " + i2 + " in radix 16 is " + ch2);
   }
}

輸出

編譯上述程式碼後,獲得的輸出為:

Char representation of 100 in radix 10 is 
Char representation of 150 in radix 16 is 

獲取超出數字和給定基數的字元表示示例

負數不屬於任何數字系統的類別,因為它在 MIN_RADIX 之前。因此,該方法返回空字元。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      int i1 = -4;
      int i2 = -1;
      ch1 = Character.forDigit(i1, 10);
      ch2 = Character.forDigit(i2, 16);
      System.out.println("Char representation of " + i1 + " in radix 10 is " + ch1);
      System.out.println("Char representation of " + i2 + " in radix 16 is " + ch2);
   }
}

輸出

執行上述程式碼後,獲得的輸出為:

Char representation of -4 in radix 10 is 
Char representation of -1 in radix 16 is 

我們可以看到負整數沒有輸出。

獲取由 ASCII 值表示的數字和給定基數的字元表示示例

即使 '9' 之後的數字由字元 'a' 到 'f' 表示,這些字元的 ASCII 值也超出基數限制。因此,返回空字元。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      int i1 = (int)'a';
      int i2 = (int)'f';
      ch1 = Character.forDigit(i1, 16);
      ch2 = Character.forDigit(i2, 16);
      System.out.println("Char representation of " + i1 + " in radix 16 is " + ch1);
      System.out.println("Char representation of " + i2 + " in radix 16 is " + ch2);
   }
}

輸出

獲得的輸出如下:

Char representation of 65 in radix 10 is .
Char representation of 88 in radix 10 is .
java_lang_character.htm
廣告

© . All rights reserved.