java.lang.Character digit(char ch, int radix)

Description

The Character.digit(char ch, int radix) java method Returns the numeric value of the character ch in the specified radix.

If the radix is not in the range MIN_RADIX ≤ radix ≤ MAX_RADIX or if the value of ch 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 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, 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.

Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the digit(int, int) method.

The digit(char ch, 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(char ch, 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(char ch, int radix)

Method Argument

Data Type Parameter Description
char ch the character to be converted.
int radix the radix.

Method Returns

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

Compatibility

Requires Java 1.0 and up

Java Character digit(char ch, int radix) Example

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

package com.javatutorialhq.java.examples;

import java.util.Scanner;

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

public class CharacterDigitCharExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter an input:");

		// use scanner to get the user input
		Scanner s = new Scanner(System.in);

		// gets the user input
		char[] value = s.nextLine().toCharArray();

		// ask for radix input
		System.out.print("Enter the radix:");

		// get the radix
		int radix = s.nextInt();
		
		// close the scanner object
		s.close();

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

	}

}

Sample Output

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

java Character digit(char ch, int radix) example output