Print ascii value in Java
In this post, you will learn how to find the ASCII value of a character in the Java programming language.
ASCII stands for American Standard Code for Information Interchange. It is a character encoding that uses numeric codes to represent characters. Each letter is assigned a number between 0 and 127. An upper and lower case character are allocated different numbers. For example, the character 'A' is assigned the decimal number 65, while 'a' is assigned decimal 97.
When a PC sends information, the keys you press or the content you send and receive is sent as a bunch of numbers. These numbers represent the characters you typed or produced. Since the scope of standard ASCII is 0 to 127, it just requires 7 bits or 1 byte of data.
There are different ways to print ascii values in Java, as we can use the int variable or type-casting to print ascii values. Here we have mentioned different examples -
Using ascii variable
Java internally converts the character value to ascii value, there is no need to use any class or method only we need to assign the character variable to int variable. In the given example, we have assigned two characters p and s in the chr1 and chr2 variables. To find the ASCII value of p and s, we have assigned chr1 and chr2 variables to the integer variables ascii1 and ascii2, respectively.
public class FindAsciiValues
{
public static void main(String[] args)
{
char chr1 = 'p';
char chr2 = 's';
int ascii1 = chr1;
int ascii2 = chr2;
System.out.println("The ASCII value of " + chr1 + " : " + ascii1);
System.out.println("The ASCII value of " + chr2 + " : " + ascii2);
}
}
Output of the above code:
The ASCII value of p : 112
The ASCII value of s : 115
We can also write the above program in the easiest way like this -
public class FindAsciiValues
{
public static void main(String[] args)
{
int ascii1 = 'p';
int ascii2 = 's';
System.out.println("The ASCII value of p : " + ascii1);
System.out.println("The ASCII value of s : " + ascii2);
}
}
The above code returns the same output -
The ASCII value of p : 112
The ASCII value of s : 115
Using Type-Casting
In the given program, we will learn how to print the ASCII value of a character by type-casting.
public class PrintAsciiValue
{
public static void main(String[] args)
{
//characters whose ASCII value to be found
char chr1 = 'r';
char chr2 = 't';
char chr3 = '*';
char chr4 = '$';
char chr5 = '&';
//casting a charter into int
int ascii1 = (int) chr1;
int ascii2 = (int) chr2;
int ascii3 = (int) chr3;
int ascii4 = (int) chr4;
int ascii5 = (int) chr5;
System.out.println("The ASCII value of " + chr1 + " is: " + ascii1);
System.out.println("The ASCII value of " + chr2 + " is: " + ascii2);
System.out.println("The ASCII value of " + chr3 + " is: " + ascii3);
System.out.println("The ASCII value of " + chr4 + " is: " + ascii4);
System.out.println("The ASCII value of " + chr5 + " is: " + ascii5);
}
}
Output of the above code:
The ASCII value of r is: 114
The ASCII value of t is: 116
The ASCII value of * is: 42
The ASCII value of $ is: 36
The ASCII value of & is: 38
Java program to find ASCII values of all characters between 1 to 50
If you want to know the ASCII values of all characters between the A to Z. We can iterate over this range using a for loop and know their ASCII values -
public class PrintAsciiValue
{
public static void main(String[] args)
{
for(char ch='A';ch<='Z';ch++)
System.out.println("The ASCII value of " + ch + " is: " + (int)ch);
}
}
Output of the above code:
The ASCII value of A is: 65
The ASCII value of B is: 66
The ASCII value of C is: 67
The ASCII value of D is: 68
The ASCII value of E is: 69
The ASCII value of F is: 70
The ASCII value of G is: 71
The ASCII value of H is: 72
The ASCII value of I is: 73
The ASCII value of J is: 74
The ASCII value of K is: 75
The ASCII value of L is: 76
The ASCII value of M is: 77
The ASCII value of N is: 78
The ASCII value of O is: 79
The ASCII value of P is: 80
The ASCII value of Q is: 81
The ASCII value of R is: 82
The ASCII value of S is: 83
The ASCII value of T is: 84
The ASCII value of U is: 85
The ASCII value of V is: 86
The ASCII value of W is: 87
The ASCII value of X is: 88
The ASCII value of Y is: 89
The ASCII value of Z is: 90
Java program to find ASCII values by taking user input
In the given program, we have asked the user to provide character to know their ascii value.
import java.util.*;
public class PrintAsciiValue
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the character : ");
char chr = sc.next().charAt(0);
int ascii = chr;
System.out.println("The ASCII value of " + chr + " is: " + ascii);
}
}
Output of the above code:
Enter the character : t
The ASCII value of t is: 116
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number