How to Convert Char to Int in Java

In this tutorial, we will learn how to convert char to int value and vice versa in Java easily.

There are three ways to convert char to int value in Java. They are as follows:

  • Get ASCII value
  • Character.getNumericValue(char)
  • String.valueOf(char)

Let’s understand each way one by one with the help of example programs.

Converting char to int in Java using ASCII value


To convert char to int numeric value in Java, assign direct char variable to int. It will return an ASCII value of a character.

Let’s create a Java program for converting char to int value by getting ASCII value.

Program code 1:

// Java Program to demonstrate the conversion of char into int value.
package javaConversion;
public class CharToIntConversion {
public static void main(String[] args) 
{  
 char ch = 'd';  
 char ch2 = '5';  
 int x = ch;  
 int y = ch2;  
 System.out.println(x);  
 System.out.println(y);  
 }
}
Output:
     100
     53

Converting char to int using Character.getNumericValue()


Character class in Java wraps a value of the primitive type char in an object. It provides a getNumericValue() method that returns an int value representing a specified Unicode character.

The general signature of this method is as:

public int getNumericValue(char ch)

This method accepts a parameter ch of char type that has to be converted.


Let’s make a simple Java program to convert char to int using Character.getNumericValue(char) method, which returns an integer value.

Program code 2:

// Java Program to demonstrate the conversion of char into int value using Character.getNumericValue().
package javaConversion;
public class CharToIntConversion {
public static void main(String[] args) 
{  
 char ch = 't';    
 int x = Character.getNumericValue(ch);
 System.out.println(x);  
 }
}
Output:
     29

Converting char to int using String.valueOf() method


Let’s create a Java program to get the integer value of specified char value using String.valueOf(char) method.

Program code 3:

// Java Program to get the integer value of specified char value using String.valueOf().
package javaConversion;
public class CharToIntConversion {
public static void main(String[] args) 
{  
 char ch = '9';    
 int x = Integer.parseInt(String.valueOf(ch)); 
 System.out.println(x); 
 System.out.println(x + 10);
 }
}
Output:
      9
      19

Convert Int to Char in Java using Typecasting


To convert int to char value in Java, we use typecasting. To convert the higher data type to lower, we perform typecasting in the program through a typecast operator (datatype).

Let’s make a Java program for converting int primitive type into char value using typecasting.

Program code 4:

// Java Program for converting int to char using typecasting.
package javaConversion;
public class IntToCharConversion {
public static void main(String[] args) 
{  
 int num = 65;  
 char ch = (char)num;  
 System.out.println(ch);
 }
}
Output:
      A

As you can observe in the program, we have stored the ASCII character of integer value in the char variable.


Let’s create another program in which we will store integer value in a single quote. Here, it will store the actual character in char variable.

Program code 5:

// Java Program for converting int to char using typecasting.
package javaConversion;
public class IntToCharConversion {
public static void main(String[] args) 
{  
 int num = '1';  
 char ch = (char)num;  
 System.out.println(ch);
 }
}
Output:
      1

Converting Int to Char in Java using Character.forDigit()


Character class in Java provides a forDigit() method to determine the character representation for a specific digit in the specified radix. The general signature of this method is as:

public char forDigit(int digit, int radix)

This method returns the actual character. If the value of radix is not a valid radix, or the value of digit is not a valid digit in the specified radix, Java returns the null character(‘\u005Cu0000’).

The radix value will be valid if it is greater than or equal to MIN_RADIX and less than or equal to MAX_RADIX. The digit value is valid if it is greater than or equal to zero and less than radix (0 <= digit < radix).

If the digit is less than 10, the method returns ‘0’ + digit. Otherwise, return the value ‘a’ + digit – 10. Let’s create a Java program based on it.

Program code 6:

// Java Program for converting int to char using forDigit() of Character class.
package javaConversion;
public class IntToCharConversion {
public static void main(String[] args) 
{  
 int REDIX = 10; // Here, redix 10 is for decimal number. 
 int num = 2;    
 char ch = Character.forDigit(num, REDIX);    
 System.out.println(ch);  
 }
}
Output:
      2

Let’s create another program in which we will use redix 16 in Character.forDigit() method to get the hexa value.

Program code 7:

// Java Program for converting int to char using forDigit() of Character class.
package javaConversion;
public class IntToCharConversion {
public static void main(String[] args) 
{  
 int REDIX = 16; // Here, redix 16 is for hexadecimal number. 
 int num = 10; 
 int num2 = 11;
 char ch1 = Character.forDigit(num, REDIX);  
 char ch2 = Character.forDigit(num2, REDIX);
 System.out.println(ch1);  
 System.out.println(ch2);
 }
}
Output:
      a
      b

As you can observe in the output, the hexadecimal code of 10 and 11 is a and b, respectively.


In this tutorial, you learned how to convert char to int value and vice versa in Java easily. Hope that you will have understood and practiced all programs based on the conversion of char to int and int to char in Java.
Thanks for reading!!!
Next ⇒ Top Java String Interview Questions⇐ Prev Next ⇒