Java Character forDigit() 方法

2024 年 11 月 6 日 | 阅读 2 分钟

character 类的 forDigit(int digit, int radix) 方法根据指定的基数确定特定数字的字符表示。如果数字的值在指定的基数中不是有效的数字,或者基数的值不是有效的基数,则返回一个空字符。如果数字小于 10,则返回 '0'+digit。否则,返回 'a'+digit-10 的值。

语法

参数

上述方法需要两个参数

需要转换为字符的数字。

基数

返回值

forDigit(int digit, int radix) 方法返回指定索引中指定数字的 char 表示。

示例 1

输出

The char representation of 5 in radix 10 is 5
The char representation of 6 in radix 16 is 6

示例 2

输出

The char representation of 5 in radix 10 is 5
The char representation of 15 in radix 16 is f
The char representation of 27 in radix 16 is r

示例 3

输出

Enter an input:4
Enter the radix:7
The character representation using radix '7' for digit 4  is : 4