Java.lang.Character.forDigit() 方法
描述
java.lang.Character.forDigit(int digit, int radix) 确定指定基数中特定数字的字符表示。 如果 radix 的值不是有效的基数,或者 digit 的值不是指定基数中的有效数字,则返回空字符('\u0000')。
radix 参数在大于或等于 MIN_RADIX 且小于或等于 MAX_RADIX 时有效。 如果 0 ≤ digit < 基数,则数字参数有效。
如果数字小于 10,则 '0' + 返回一个数字。 否则,值为 'a' + 数字 − 10 被退回。
声明
以下是 java.lang.Character.forDigit() 方法的声明。
public static char forDigit(int digit, int radix)
参数
digit − 要转换为字符的数字
radix − 基数
返回值
此方法返回指定基数中指定数字的 char 表示形式。
异常
NA
示例
下面的例子展示了 lang.Character.forDigit() 方法的使用。
package com.tutorialspoint;
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

