Get the ASCII Value of a Character in Java

06 May 2023 Balmiki Mandal 0 Core Java

Get the ASCII Value of a Character in Java

In Java, obtaining the ASCII value of a character is as simple as calling the built-in charAt() method on a String object. The charAt() method takes an integer parameter and returns the character at the specified index within the string.

Here is a simple example of how to use the charAt() method to get the ASCII value of a character:

String s = "Hello World!";
char c = s.charAt(0);
int asciiValue = (int) c;
System.out.println("The ASCII value of " + c + " is: " + asciiValue);

This code will print out the following to the console:

The ASCII value of H is: 72

The (int) cast is required in order to convert the character data type to an integer. Once we have the ASCII character code, we can use it for whatever application we need.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.