Situatie
ASCII acronym for American Standard Code for Information Interchange. It is a 7-bit character set contains 128 (0 to 127) characters. It represents the numerical value of a character. For example, the ASCII value of A is 65.
In this section, we will learn how to print ASCII value or code through a Java program.
There are two ways to print ASCII value in JAVA
- Assigning a Variable to the int Variable
- Using Type-Casting
Assigning a Variable to the int Variable
To print the ASCII value of a character, we need not use any method or class. Java internally converts the character value to an ASCII value. In the following program, we have assigned two characters a and b in the ch1 and ch2 variables, respectively. To find the ASCII value of a and b, we have assigned ch1 and ch2 variables to the integer variables asciivalue1 and asciivalue2, respectively. Finally, we have printed the variable asciivalue1 and asciivalue2 in which ASCII values of the characters are stored.
Backup
- public class PrintAsciiValueExample1
- {
- public static void main(String[] args)
- {
- // character whose ASCII value to be found
- char ch1 = ‘a’;
- char ch2 = ‘b’;
- // variable that stores the integer value of the character
- int asciivalue1 = ch1;
- int asciivalue2 = ch2;
- System.out.println(“The ASCII value of “ + ch1 + ” is: “ + asciivalue1);
- System.out.println(“The ASCII value of “ + ch2 + ” is: “ + asciivalue2);
- }
- }
Output:
The ASCII value of a is: 97 The ASCII value of b is: 98
Leave A Comment?