How to get ASCII value of char in Java
Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday variable undertaking operations. To understand this situation, you ought to have an understanding of the subsequent Java programming subjects:
1. Java Date Type (Primitive)
2. Java Hello World Program
The full form of ASCII is American Standard Code Information Interchange. The ASCII code contain 7-bit characters. The ASCII code represent the numeric value of any given character. The 7-bit set contain 128 characters which is between 0 to 127.instance- The ASCII fee of H is seventy-two, ASCII value of B is 66. we can print the ASCII fee of man or woman A to Z, a to z, and special characters (@, #, $, ! and so on). To print the ASCII price, there are approaches.
To print the ASCII cost of a person, we need not apply any method or magnificence. Java programming language can convert the person's price internally to an ASCII value. allow observed the ASCII cost of a man or woman using the Java software program.
For print the ASCII value, there is two ways. These are-
- Assigning a Variable to the int Variable
- Using Type Casting
1. Assigning a Variable to the int Variable:
Now right here we speak approximately how we can assign a Variable to the int Variable. To print the ASCII price of a man or woman, we want no longer to use any method or magnificence. Java programming language can converts the individual fee internally to an ASCII cost. Permit found the ASCII fee of an individual the usage of the Java software.
In the below application, we have assigned characters a and b within the ch1 and ch2 variables, respectively. To find out the ASCII cost of a and b, we have set the ch1 and ch2 variables to the integer variables asciivalue1 and asciivalue2, respectively. ultimately, we've published the variables asciivalue1 and asciivalue2 in which ASCII values of the characters are stored.
Example 1 - Here we give an example of assign a Variable to the int Variable.
public class AsciiValue
{
public static void main(String[] String)
{
//assigned two characters for found the ASCII value of those character
int ch1 = 'A';
int ch2 = 'B';
//Print the result
System.out.println("The ASCII value of A is: "+ch1);
System.out.println("The ASCII value of B is: "+ch2);
}
}
Output: For finding the result, we compile the above program and also run it.
The ASCII value of A is: 65
The ASCII value of B is: 66
Example 2 - Here we give an example of assign a Variable to the int Variable.
public class PrintAsciiValue
{
public static void main (String[] args)
{
//assigned two characters for found the ASCII value of those character
char ch1 = 'H';
char ch2 = 'k';
// given variable stores the character’s integer value
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: For finding the result, we compile the above program and also run it.
The ASCII value of H is: 72
The ASCII value of K is: 75
2. Using Type Casting:
Now here we discuss about the how we can using the Type Casting. It is a manner to convert a variable into some other data. In the following software, we have declared variables ch1 and ch2 of kind char having the man or woman cost of a and b respectively. within the subsequent two lines, we have forged char type to int kind the use of (int). In the end, we have revealed the variables ascii1 and ascii2 where ASCII values of the characters are saved.
Example 1 - Here we give an example of using the Type Casting.
import java.util.Scanner;
public class AsciiValue
{
public static void main(String args[])
{
System.out.print("Enter a character: ");
Scanner sc = new Scanner(System.in);
char chr = sc.next().charAt(0);
int asciiValue = chr;
System.out.println("ASCII value of " +chr+ " is: "+asciiValue);
}
}
Output: For finding the result, we compile the above program and also run it.
Enter a character: r
The ASCII value of r is: 82
Example 2 - Here we give an example of using the Type Casting.
public class PrintAsciiValue
{
public static void main(String [] args)
{
//assigned two characters for found the ASCII value of those character
char ch = ‘f’;
int ascii = ch;
int castAscii = (int) ch;
//Print the result
System.out.println(“The ASCII value of ” + ch + " is: "+asciiValue);
System.out.println(“The ASCII value of ” + ch + " is: "+ castAscii);
}
}
Output: For finding the result, we compile the above program and also run it.
The ASCII value of f is: 70
The ASCII value of f is: 70
So, right here we speak approximately the way to get ASCII fee of char in Java. We also give some examples of it and also provide the output of the given program for better understand.