Java - Character forDigit() Method



The Java Character forDigit() determines the character representation for a specific digit in the specified radix.

A radix is defined as the number of unique digits, including zero, present in a numeric system. The most common radix value is 10, representing the decimal system (0-9).

The advantage of using this method is that it lets you produce digits that not actually "digits". For example, in a hexadecimal numeric system, the numbers after '9' are represented using characters from 'A' to 'F'.

Syntax

Following is the syntax for Java Character forDigit() method

public static char forDigit(int digit, int radix)

Parameters

  • digit − the number to convert to a character. This argument is valid if 0 ≤ digit < radix.

  • radix − the radix. This argument is valid if MIN_RADIX ≤ radix ≤ MAX_RADIX.

Return Value

This method returns the char representation of the specified digit in the specified radix. Some of the special cases include −

  • If the value of radix is not a valid radix, or the value of digit is not a valid digit in the specified radix, the null character ('\u0000') is returned.

  • If the digit is less than 10, then '0' + digit is returned. Otherwise, the value 'a' + digit − 10 is returned.

Example

The following example shows the usage of Java Character forDigit() method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 character primitives ch1, ch2
      char ch1, ch2;

      // create 2 int primitives i1, i2 and assign values
      int i1 = 3;
      int i2 = 14;

      // assign char representation of i1, i2 to ch1, ch2 using radix
      ch1 = Character.forDigit(i1, 10);
      ch2 = Character.forDigit(i2, 16);
      String str1 = "Char representation of " + i1 + " in radix 10 is " + ch1;
      String str2 = "Char representation of " + i2 + " in radix 16 is " + ch2;

      // print ch1, ch2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Char representation of 3 in radix 10 is 3
Char representation of 14 in radix 16 is e

Example

In another example, if the arguments passed to the method are exceeding the MAX_RADIX, the return value will be a null character.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 character primitives ch1, ch2
      char ch1, ch2;

      // create 2 int primitives i1, i2 and assign values
      int i1 = 15;
      int i2 = 20;

      // assign char representation of i1, i2 to ch1, ch2 using radix
      ch1 = Character.forDigit(i1, 10);
      ch2 = Character.forDigit(i2, 16);

      // print ch1, ch2 values
      System.out.println("Char representation of " + i1 + " in radix 10 is " + ch1);
      System.out.println("Char representation of " + i2 + " in radix 16 is " + ch2);
   }
}

Output

On compiling the code above, the output is obtained as −

Char representation of 100 in radix 10 is 
Char representation of 150 in radix 16 is 

Example

Negative numbers do not fall under the categories of any numeric system, as it precedes the MIN_RADIX. Hence, a null character is returned by the method.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      int i1 = -4;
      int i2 = -1;
      ch1 = Character.forDigit(i1, 10);
      ch2 = Character.forDigit(i2, 16);
      System.out.println("Char representation of " + i1 + " in radix 10 is " + ch1);
      System.out.println("Char representation of " + i2 + " in radix 16 is " + ch2);
   }
}

Output

Once the above code is executed, the output is achieved as −

Char representation of -4 in radix 10 is 
Char representation of -1 in radix 16 is 

As we can see the output does not exist for negative integers.

Example

Even though the digits after '9' are represented by the characters from 'a' to 'f', the ASCII values of these characters fall outside the radix limit. Hence, a null character is returned.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      int i1 = (int)'a';
      int i2 = (int)'f';
      ch1 = Character.forDigit(i1, 16);
      ch2 = Character.forDigit(i2, 16);
      System.out.println("Char representation of " + i1 + " in radix 16 is " + ch1);
      System.out.println("Char representation of " + i2 + " in radix 16 is " + ch2);
   }
}

Output

The output is obtained as follows −

Char representation of 65 in radix 10 is .
Char representation of 88 in radix 10 is .
java_lang_character.htm
Advertisements