Low Orbit Flux Logo 2 F

Java How To Get Ascii Value Of Character

If you are wondering how to get the ASCII value of a character in Java the good news is that this is extremely easy. Whatever your reason for needing to do this we’re going to show you how. Hopefully you find this useful.

Answer: To get the ASCII value of a character in Java you just need to assign that character to an int like this:

int ascii1 = ch1;

Here is an example. You can choose to cast the variables or not.

class Test1 {
    public static void main( String args[] ) {

        char ch1 = 'a';
        char ch2 = 'b';
        char ch3 = 'c';

        // easiest way - just assign to integers
        int ascii1 = ch1;
        int ascii2 = ch2;
        int ascii3 = ch3;


        // you can cast the variables but this is optional
        int castAscii4 = (int) ch1;
        int castAscii5 = (int) ch2;
        int castAscii6 = (int) ch3;

        System.out.println("ASCII value : " + ascii1);
        System.out.println("ASCII value : " + ascii2);
        System.out.println("ASCII value : " + ascii3);
        System.out.println("ASCII value : " + castAscii4);
        System.out.println("ASCII value : " + castAscii5);
        System.out.println("ASCII value : " + castAscii6);

    }
}