Java Character forDigit() Method

The forDigit(int digit, int radix) method of character class determines the character representation for a specific digit in the specified radix. If the value of the digit is not a valid digit in the specified radix or if the value of the radix is not a valid radix then a null character is returned. If the digit is less than 10, the '0'+digit is returned. Otherwise, the value 'a'+digit-10 is returned.

Syntax

Parameter

The above method requires two parameters:

The digit that needs to be converted to a character.

The radix

Return Value

The forDigit(int digit, int radix) method returns the char representation of the specified digit in the specified index.

Example 1

Output:

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

Example 2

Output:

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

Example 3

Output:

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