How to Convert int to char in Java

To convert a higher data type to lower data type, we need to do typecasting. Casting is also required when we want to convert ASCII value into character. It is called an explicit conversion. There are two methods to convert int to char:

  • Using typecast operator
  • Using forDigit() method

Using typecast operator

Casting is when we explicitly convert from one primitive data type, or a class, to another.


In this example, we have initialized 65 to the variable a. 65 is the ASCII value of A. We cast the number from an integer to char, converting it to the letter A.



Remember: If you are assigning 1 to a variable and want to convert it into character, then nothing will be print on the console. Because it will store ASCII value of 1 that is SOH (start of heading) which is a non-printable character.


In the following example, we have done the same as above mentioned. We have initialized 1 to the variable i of type int. In the next statement, we performed casting. In the println statement, we are trying to print the converted value of 1, but it will not print the character ch.


To remove the above problem, we will add ‘0’ (as a character not as a digit) to the integer i during casting. It returns the actual value in the character form. The ASCII value of ‘0’ is 48. When we add 48 to 1, it becomes 49, which is the ASCII value of 1.



When we initialize ‘1’ as a character to the variable i of type int, it will store actual character in ch variable and print the same.



Using forDigit() method

forDigit() is a built-in static method of Character class which belongs to java.lang package. It parses two arguments digit and radix and returns the character representation of digit in specified radix format.  It returns null if the digit or radix are invalid numbers.  The signature of the method is given below.

public static char forDigit(int digit, int radix)

Invalid conversions

The following conversions returns null.

char ch2=Character.forDigit(3,2);                    //Binary radix

char ch2=Character.forDigit(9,8);                  //Octal radix

char ch2=Character.forDigit(10,10);      //decimal radix    

char ch2=Character.forDigit(16,16);  //Hexa radix

Valid Conversions

char ch2=Character.forDigit(0,10);    

char ch2=Character.forDigit(15,16);    //returns f because decimal value of f is 15

We can conclude from the above conversions that digit must be less than the radix.



Pin It on Pinterest

Share This