Java.lang.Character.digit() 方法
描述
java.lang.Character.digit(char ch, int radix) 返回指定基数中字符 ch 的数值。
如果基数不在 MIN_RADIX ≤ radix ≤ MAX_RADIX 范围内,或者 ch 的值不是指定基数中的有效数字,则返回 -1。 如果以下至少一项为真,则字符是有效数字 −
isDigit 方法对字符为真且字符的 Unicode 十进制数字值(或其单字符分解)小于指定的基数。 在这种情况下,返回十进制数字值。
The character is one of the uppercase Latin letters 'A' through 'Z' and its code is less than radix + 'A' − 10. In this case, ch − 'A' + 10 is returned.
The character is one of the lowercase Latin letters 'a' through 'z' and its code is less than radix + 'a' − 10. In this case, ch − 'a' + 10 is returned.
The character is one of the fullwidth uppercase Latin letters A ('\uFF21') through Z ('\uFF3A') and its code is less than radix + '\uFF21' − 10. In this case, ch − '\uFF21' + 10 is returned.
The character is one of the fullwidth lowercase Latin letters a ('\uFF41') through z ('\uFF5A') and its code is less than radix + '\uFF41' − 10. In this case, ch − '\uFF41' + 10 is returned.
声明
以下是 java.lang.Character.digit() 方法的声明。
public static int digit(char ch, int radix)
参数
ch − 要转换的字符
radix − 基数
返回值
该方法返回指定基数中的字符所代表的数值。
异常
NA
示例
下面的例子展示了 lang.Character.digit() 方法的使用。
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create 2 character primitives ch1, ch2 char ch1, ch2; // assign values to ch1, ch2 ch1 = '9'; ch2 = '5'; // create 2 int primitives i1, i2 int i1, i2; // assign numeric value of ch1, ch2 to i1, i2 using radix i1 = Character.digit(ch1, 2); i2 = Character.digit(ch2, 10); String str1 = "Numeric value of " + ch1 + " in radix 2 is " + i1; String str2 = "Numeric value of " + ch2 + " in radix 10 is " + i2; // print i1, i2 values System.out.println( str1 ); System.out.println( str2 ); } }
让我们编译并运行上面的程序,这将产生下面的结果 −
Numeric value of 9 in radix 2 is -1 Numeric value of 5 in radix 10 is 5