Q:

Java Convert char to int

belongs to collection: Java Conversion Programs

0

We can convert char to int in java using various ways. If we direct assign char variable to int, it will return ASCII value of given character.

If char variable contains int value, we can get the int value by calling Character.getNumericValue(char) method. Alternatively, we can use String.valueOf(char) method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

1) Java char to int Example: Get ASCII value

Let's see the simple code to convert char to int in java.

public class CharToIntExample1{  
public static void main(String args[]){  
char c='a';  
char c2='1';  
int a=c;  
int b=c2;  
System.out.println(a);  
System.out.println(b);  
}}  

 

Output:

97
49

 

2) Java char to int Example: Character.getNumericValue()

Let's see the simple code to convert char to int in java using Character.getNumericValue(char) method which returns an integer value.

public class CharToIntExample2{  
public static void main(String args[]){  
char c='1';  
int a=Character.getNumericValue(c);  
System.out.println(a);  
}}  

 

Output:

1

 

3) Java char to int Example: String.valueOf()

Let's see another example which returns integer value of specified char value using String.valueOf(char) method.

public class CharToIntExample3{  
public static void main(String args[]){  
char c='1';  
int a=Integer.parseInt(String.valueOf(c));  
System.out.println(a);  
}}  

 

Output:

1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Convert int to char... >>
<< Java Convert double to int...