java.lang.Character digit(int codePoint, int radix)

Description

The Character.digit(int codePoint, int radix) java method returns the numeric value of the specified character (Unicode code point) in the specified radix.

If the radix is not in the range MIN_RADIX ≤ radix ≤ MAX_RADIX or if the character is not a valid digit in the specified radix, -1 is returned. A character is a valid digit if at least one of the following is true:

  • The method isDigit(codePoint) is true of the character and the Unicode decimal digit value of the character (or its single-character decomposition) is less than the specified radix. In this case the decimal digit value is returned.
  • 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.

The digit(int codePoint, int radix) method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:

Character.digit(int codePoint, int radix)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java digit() method non statically.

Method Syntax

public static int digit(int codePoint, int radix)

Method Argument

Data Type Parameter Description
int codePoint the character (Unicode code point) to be converted.
int radix the radix.

Method Returns

The digit(int codePoint, int radix) method of Character class returns the numeric value represented by the character in the specified radix.

Compatibility

Requires Java 1.5 and up

Java Character digit(int codePoint, int radix) Example

Below is a simple java example on the usage of digit(int codePoint, int radix) method of Character class.

package com.javatutorialhq.java.examples;

/*
 * This example source code demonstrates the use of 
 * digit(int codePoint, int radix) method of Character class.
 */

public class CharacterDigitCodePointExample {

	public static void main(String[] args) {

		// initialize a codepoint
		int codepoint = 65;

		// initialize the radix
		int radix = 16;
		
		// convert codepoint to char
		char ch = (char) codepoint;	

		// convert user input to digit
		int digit = Character.digit(codepoint,radix);
		
		// print the result
		System.out.println("character '" + ch + "' numeric value is " 
				+ digit+" using radix "+radix);
		
	}

}

Sample Output

Below is the sample output when you run the above example.

java Character digit(int codePoint, int radix) example output