How to convert char to int in Java
Example:
Input: ch='A'
Output: 65
Approach: 1:
Java
public class CharToInt {public static void main(String[] args) {char ch = 'A';int intv = charToInt(ch);System.out.println("int is " + intv);}private static int charToInt(char ch) {return (int) ch;}}
Approach : 2: Using getNumericValue()
Java
public class CharToInt {public static void main(String[] args) {char ch = '1';int intv = charToInt(ch);System.out.println("int is " + intv);}private static int charToInt(char ch) {return Character.getNumericValue(ch);}}
No comments:
Post a Comment