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.
Example
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.
public class inTocharExample { public static void main(String args[]) { int a=65; char ch=(char)a; System.out.println("The character value of an integer is: "+ch); } }
Output
The character value of an integer is: 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.
Example
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.
public class inTocharExample1 { public static void main(String args[]) { int i=1; char ch=(char)i; System.out.println("Nothing will be print "+ch); } }
Output
Nothing will be print
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.
Example
public class inTocharExample2 { public static void main(String args[]) { int i=1; char ch=(char)(i+'0'); System.out.println("Character value is: " +ch); } }
Output
Character value is: 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.
Example
public class inTocharExample3 { public static void main(String args[]) { int i='1'; char ch=(char)i; System.out.println("Character value is: " +ch); } }
Output
Character value is: 1
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.
Example
public class inTocharExample4 { public static void main(String args[]) { char ch1=Character.forDigit(9,10); //Octal radix format char ch2=Character.forDigit(11,16); //Hexa radix format System.out.println("9 representations in radix 10 format: "+ch1); System.out.println("11 representations in radix 16 format: "+ch2); } }
Output
9 representations in radix 10 format: 9 11 representations in radix 16 format: b