Java

How to Get the ASCII Value of a Character in Java

The “ASCII character” representation is vital as it is universally understood and consumes less space of only 256 characters. Its significance can be analyzed because before ASCII came into effect, the systems of different makes and models could not communicate. Therefore, this particular representation is of great aid in providing a link between the PC screen and the hard drive.

This blog will elaborate on the approaches to fetch the “ASCII” character value in Java.

What is an ASCII Value?

The “ASCII” value allocates letters, numbers, characters, and symbols to a position in the “256” available positions in 8-bit code.

How to Get/Fetch the ASCII Value of a Character in Java?

To fetch the “ASCII” character value in Java, apply the following approaches:

Approach 1: Get the ASCII Character Value in Java By Casting Data Types

In this approach, the “ASCII” value can be fetched by casting the specified character from the “character” data type to the “integer” data type.

Example

Let’s move on to the below-provided example:

char givenCharacter = 'd';
int asciiValue = givenCharacter;
System.out.println("The ASCII value of the character is: " + asciiValue);

 
In the above lines of code:

    • Firstly, specify the stated character to fetch its corresponding ASCII value.
    • In the next step, allocate the specified character to the “int” data type.
    • Lastly, display the resultant “ASCII” value with respect to the initialized character.

Output


In this output, it can be observed that the corresponding “ASCII value(100)” against the character “d” is retrieved.

Approach 2: Get the ASCII Value of All the String Characters in Java Using “for” Loop and “charAt()” Method

The “for” loop is used to iterate through all the contained elements, and the “charAt()” method gives the character at the specified index within the string. These approaches can be applied in combination to iterate through the string characters one by one and fetch the “ASCII” value against each of the string characters.

Syntax

charAt(int ind)

 
In the above-given syntax:

    • ind” refers to the character index.

Example

Let’s overview the following example to fetch all the corresponding “ASCII” values:

String givenString = "Linuxhint";
System.out.println("The given string is: "+ givenString);
System.out.println("The ASCII values of all the string characters are: ");
for(int i= 0;i< givenString.length();i++){
char ascii = givenString.charAt(i);
System.out.println((int)ascii);

 
In the above code block:

    • Firstly, initialize the specified string value and display it.
    • After that, apply the “for” loop to iterate along the string characters one by one.
    • Now, associate the “charAt()” method with the specified string and return the ASCII values of all the iterated string characters.
    • Lastly, cast them to integer via “int” and log them on the console.

Output


In this outcome, it can be analyzed that all the string characters are iterated, and their corresponding “ASCII” values are returned one by one.

Conclusion

To get the ASCII value of a character in Java, cast the data types or apply the combined “for” loop and “charAt()” method. The former approach fetches the “ASCII” value by allocating the integer data type to the specified character. The latter approach iterates through all the string characters and returns their corresponding “ASCII” representation. This blog discussed the procedure of fetching the ASCII value of a character using Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.