Java: Convert Char to ASCII

We can convert char to ASCII in Java by saving the char value as an int.

package org.code2care.examples;

public class CharToAsciiExample {

    public static void main(String[] args) {
        char c = 'z';
        int charToAscii = c;
        System.out.println(charToAscii);
    }
}
Output: 122
Java Convert Char to Ascii Example

From Java 8 or above we can make use of the getNumericValue(char) static method to get the numeric value associated with the given character according to Unicode specifications.

Example:
public class CharToUnicodeValueExample {

    public static void main(String[] args) {
        char c = 'A';
        int charToAscii = Character.getNumericValue(c);
        System.out.println(charToAscii);
    }
}
Output: 10

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!