Java.lang.Character.forDigit() Method


Advertisements


Description

The java.lang.Character.forDigit(int digit, int radix) determines the character representation for a specific digit in the specified radix. 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.

The radix argument is valid if it is greater than or equal to MIN_RADIX and less than or equal to MAX_RADIX. The digit argument is valid if 0 ≤ digit < radix.

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

Declaration

Following is the declaration for java.lang.Character.forDigit() method

public static char forDigit(int digit, int radix)

Parameters

  • digit − the number to convert to a character

  • radix − the radix

Return Value

This method returns the char representation of the specified digit in the specified radix.

Exception

NA

Example

The following example shows the usage of lang.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 );
   }
}

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

java_lang_character.htm

Advertisements