Java.lang.Character.digit() 方法

描述

java.lang.Character.digit(int codePoint, int radix) 返回指定基数中指定字符(Unicode 代码点)的数值。

如果基数不在 MIN_RADIX ≤ radix ≤ MAX_RADIX 范围内,或者字符不是指定基数中的有效数字,则返回 -1。 如果以下至少一项为真,则字符是有效数字 −

  • isDigit(codePoint) 方法对字符为真且字符的 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, codePoint − '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, codePoint − '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, codePoint − '\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, codePoint − '\uFF41' + 10 is returned.


声明

以下是 java.lang.Character.digit() 方法的声明。

public static int digit(int codePoint, int radix)

参数

  • codePoint − 要转换的字符(Unicode 代码点)

  • radix − the radix


返回值

该方法返回指定基数中的字符所代表的数值。


异常

NA


示例

下面的例子展示了 lang.Character.digit() 方法的使用。

package com.tutorialspoint;

import java.lang.*;

public class CharacterDemo {

   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x0034;
      cp2 = 0x004a;

      // create 2 int primitives i1, i2
      int i1, i2;

      // assign numeric value of cp1, cp2 to i1, i2 using radix
      i1 = Character.digit(cp1, 8);
      i2 = Character.digit(cp2, 8);

      String str1 = "Numeric value of cp1 in radix 8 is " + i1;
      String str2 = "Numeric value of cp2 in radix 8 is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

Numeric value of cp1 in radix 8 is 4
Numeric value of cp2 in radix 8 is -1