Integer: The Integer or int data type is a 32-bit signed two’s complement integer. Its value-range lies between – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is – 2,147,483,648 and maximum value is 2,147,483,647. Its default value is 0. The int data type is generally used as a default data type for integral values unless there is no problem with memory.
Example: int a = 10
Character: 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.
Example: char ch = 'c'
Approaches
There are numerous approaches to do the conversion of Char datatype to 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
This method uses TypeCasting to get the ASCII value of the given character. The respective integer is calculated from this ASCII value by subtracting it from the ASCII value of 0. In other words, this method converts the char to int by finding the difference between the ASCII value of this char and the ASCII value of 0.
Example:
// Java program to convert
// char to int using ASCII value
class GFG {
public static void main(String[] args)
{
// Initializing a character(ch)
char ch = '3';
System.out.println("char value: " + ch);
// Converting ch to it's int value
int a = ch - '0';
System.out.println("int value: " + a);
}
}
Output
char value: 3
int value: 3
2. Using String.valueOf() method
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 below program illustrates the use of the valueOf() method.
Example:
// Java program to convert
// char to int using String.valueOf()
class GFG {
public static void main(String[] args)
{
// Initializing a character(ch)
char ch = '3';
System.out.println("char value: " + ch);
// Converting the character to it's int value
int a = Integer.parseInt(String.valueOf(ch));
System.out.println("int value: " + a);
}
}
Output
char value: 3
int value: 3
3. Using Character.getNumericValue() method
The getNumericValue() method of class Character is used to get the integer value of any specific character. For example, the character ‘9’ will return an int having a value of 9. The below program illustrates the use of getNumericValue() method.
Example:
// Java program to convert char to int
// using Character.getNumericValue()
class GFG {
public static void main(String[] args)
{
// Initializing a character(ch)
char ch = '3';
System.out.println("char value: " + ch);
// Converting the Character to it's int value
int a = Character.getNumericValue(ch);
System.out.println("int value: " + a);
}
}
Output
char value: 3
int value: 3
ref: https://www.geeksforgeeks.org/java-program-to-convert-char-to-int/
Character.digit(char ch, int radix)
.If you want to constrain yourself to digits 0-9 you could writeint x = Character.digit(element.charAt(2), 10);
– Moujik