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) is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. 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