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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap