Java Program to Convert Char to Int (original) (raw)
Last Updated : 23 Apr, 2025
Given a **char value, and our task is to **convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.
**Examples of Conversion from Char to Int:
**Input : ch = ‘3’
**Output : 3**Input : ch = ‘9’
**Output : 9
The char data type is a single 16-bit Unicode character. Its value range lies between ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive). The char data type is used to store characters. So internally, the char values are stored as an ASCII value, for example, the character ‘a is stored as 97(Unicode value). So we can convert just by showing its Unicode value.
Methods to Convert Char to Int in Java
There are numerous approaches to the conversion of the Char datatype to the Integer (int) datatype. A few of them are listed below.
- Using ASCII Values
- Using String.valueOf() Method
- Using Character.getNumericValue() Method
**1. Using ASCII values
We can convert the char value to an int value using type casting (changing the data type from char to int ). This is the easiest way to convert the Character to an Integer value. Now we know that the if char ‘0’ is 48 so the ‘1’ would be 49 and so on so if we want to make it integer to we can convert it by actual number ‘3’-‘0′(51-48) = 3 (The actual value) so we can use this calculation to convert the value.
**Example: Converting char to int using Typecasting in Java
Java `
// Java Program to Convert Char to Int // Using ASCII value class Geeks { // Main driver method public static void main(String[] args) { // Initializing a character char num = '3';
// Printing the character value
System.out.println("char value: " + num);
// Converting character to its integer value
int n = num - '0';
// Printing the integer value
System.out.println("int value: " + n);
}
}
`
Output
char value: 3 char value: a int value: 97 int value: 3
**2. Using the String.valueOf()
The method **valueOf() of class String can convert various types of values to a String value. It can convert int, char, long, boolean, float, double, object, and char array to String, which can be converted to an int value by using the Integer.parseInt() method. The program below illustrates the use of the valueOf() method.
**Example: Converting char to int using the method **String.valueOf() in Java.
Java `
// Java program to convert Char to Int // Using valueOf() method of String Class class Geeks { // Main driver method public static void main(String[] args) {
// Declaring and initializing a character
char ch = '3';
// Printing the character value
System.out.println("char value: " + ch);
// Converting the character to it's integer value
// using valueOf() method
int a = Integer.parseInt(String.valueOf(ch));
// Printing the integral value
// corresponding to its character value
System.out.println("int value: " + a);
}
}
`
Output
char value: 3 int value: 3
**3. Using getNumericValue()
The **getNumericValue() method present in the Character class is used to get the integer value of any specific character. For example, the character ‘1’ will return an int having a value of 1.
**Example: Converting char to int using the method **getNumericValue().
Java `
// Java Program to Convert Character to Integer // Using getNumericValue() method of Character Class
// Driver Class class Geeks { // Main driver method public static void main(String[] args) { // Declaring and initializing a character char ch = '3';
// Displaying above character on console
System.out.println("char value: " + ch);
// Converting the Character to it's int value
// using getNumericValue() method of Character Class
int a = Character.getNumericValue(ch);
// Printing the corresponding integral value
System.out.println("int value: " + a);
}
}
`
Output
char value: 3 int value: 3