java.lang.Character.forDigit(int digit, int radix)方法範例


java.lang.Character.forDigit(int digit, int radix) 確定在指定基數中的一個特定數位以字元表示。如果基數的值不是一個有效的基數,或者數位的值是不是在指定基數的有效位數,空字元('u0000“)返回。

radix引數是有效的,如果它大於或等於MIN_RADIX和小於或等於MAX_RADIX。數位引數是有效的,如果0 ≤ digit < radix。

如果該數位小於10,則“0”+位被返回。否則,值'a'+ 數位- 10返回。

宣告

以下是java.lang.Character.forDigit()方法的宣告

public static char forDigit(int digit, int radix)

引數

  • digit - 數位轉換為字元

  • radix - 基數

返回值

此方法返回指定基數數位的字元表示。

異常

  • NA

例子

下面的例子顯示lang.Character.forDigit()方法的使用。

package com.yiibai;

import java.lang.*;

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