Examples
- Hello World Program in Java (Print Hello World in Java)
- Java Program to Add Two Numbers (Java Sum / Addition)
- Find Greatest of Three Numbers in Java (Largest Number Program)
- Prime Number Program in Java (Code to Check Prime or Not)
- Java Program for Fibonacci Series (Using for, while, recursion, scanner)
- Factorial Program in Java (Find Factorial of a Number in Java)
- How to Find Sum of Digits of a Number in Java?
- How to Reverse a Number in Java? Program & Examples
- How to Swap Two Numbers in Java? Programs With/Without Third Variable
- Even Odd Program in Java (Program to Check Number is Even or Odd)
- Vowel and Consonant Program in Java
- Java Program for Quadratic Equation (Find Roots With 3 Ways)
- Find Frequency of Characters in a String in Java (4 Ways)
- Remove Space from String in Java (Remove Whitespace)
- String Null Check in Java (String is Empty or Null) - 5 Ways
- How to Print String in Java? 6 Methods
- How to Get ASCII Value of Char in Java? Find ASCII Value
How to Get ASCII Value of Char in Java? Find ASCII Value
In computer programming, every character you see on your screen is represented by a numeric value. These numeric values are an integral part of how computers process and store text, and they are especially crucial when working with character data in programming languages like Java. Hence, it is important to learn how to get ASCII value of a character in Java.
What is ASCII Value?
ASCII, which stands for the American Standard Code for Information Interchange, is a character encoding standard that assigns a unique numeric value to each character, including letters, digits, punctuation marks, and control characters.
Understanding ASCII values is like deciphering a secret code that computers use to represent characters. Each character is mapped to a specific number between 0 and 127, making it easy for computers to work with text-based data.
For example, the ASCII value of the letter 'A' is 65, 'a' is 97, '1' is 49, and so on.
Use Cases of Getting ASCII Values in Java
-
Character Classification: ASCII values enable programmers to classify characters easily. For instance, you can check whether a character is a digit, a letter, or a special symbol by examining its ASCII value.
-
String Manipulation: When working with strings, you may need to convert characters to their ASCII values to perform specific operations or transformations.
-
Encryption and Encoding: ASCII values play a crucial role in encryption algorithms and encoding schemes like Base64, where characters are converted to their corresponding ASCII values and manipulated for data security and transmission.
-
Parsing and Validation: When processing input from users or external sources, you can use ASCII values to validate and parse text effectively.
-
Sorting and Comparisons: Sorting algorithms often rely on comparing ASCII values to arrange characters or strings in the desired order.
In this tutorial, we will explore various methods to find the ASCII values of characters in Java.
Concepts to Learn:
Get ASCII Value of Char Using Type Casting
Here is a program on how to print ASCII value in Java using type casting:
Code
public class ASCIIToCharExample {
public static void main(String[] args) {
char ch = 'A';
int asciiValue = (int) ch;
System.out.println("The ASCII value of '" + ch + "' is: " + asciiValue);
}
}
Output
The ASCII value of 'A' is: 65
Explanation
In this method, we use Java-type casting to convert a char to an int. By simply enclosing the char variable within (int), we convert the character 'A' to its ASCII value, which is 65.
Type casting works because char is essentially a 16-bit unsigned integer in Java, and when cast to an int, it retains its numeric value.
Java ASCII Value of Character Using Character.getNumericValue() method
This method is particularly useful when you want to ensure that the character is a valid numeric digit before obtaining its ASCII value, as it handles non-numeric characters gracefully.
Code
public class ASCIIToCharExample {
public static void main(String[] args) {
char ch = '7';
int asciiValue = Character.getNumericValue(ch);
if (asciiValue != -1) {
System.out.println("The ASCII value of '" + ch + "' is: " + asciiValue);
} else {
System.out.println("'" + ch + "' is not a valid numeric character.");
}
}
}
Output
The ASCII value of '7' is: 7
Explanation
In this method, we use the Character.getNumericValue() method to obtain the ASCII value of a character in Java. This method primarily works for numeric characters (0-9) and a few other special characters (like letters 'a' to 'z' or 'A' to 'Z').
If the character is a valid numeric character, this method returns its corresponding numeric value. In the example, when '7' is provided as input, the method returns 7, indicating that '7' has an ASCII value of 7.
However, if the character is not a valid numeric character, the method returns -1, as shown when 'A' is provided as input.
Find ASCII Value in Java Using Bitwise Operations
This method is a bit more low-level compared to the previous methods but allows for direct manipulation of bits to obtain the ASCII value of a character.
Code
public class ASCIIToCharExample {
public static void main(String[] args) {
char ch = 'B';
int asciiValue = ch & 0xFF;
System.out.println("The ASCII value of '" + ch + "' is: " + asciiValue);
}
}
Output
The ASCII value of 'B' is: 66
Explanation
In this method, we use Java bitwise operations to extract the ASCII value of a character. By performing a bitwise AND operation (&) between the character ch and the hexadecimal value 0xFF (which is 255 in decimal), we obtain the ASCII value of the character.
Here's how it works:
-
The & operator compares each bit of the character and 0xFF and retains only the bits that are common to both.
-
This effectively strips away any higher-order bits (bits beyond 8th position) and leaves us with the ASCII value in the lower 8 bits.
In the example, when 'B' is provided as input, the bitwise AND operation results in the binary value 01000010, which is equivalent to 66 in decimal, representing the ASCII value of 'B'.
Print ASCII Values of Alphabets in Java (A-Z & a-z)
You can print the ASCII values of alphabets in Java from ‘A to Z’ and ‘a to z’ using a simple loop.
Code
public class PrintAlphabetASCII {
public static void main(String[] args) {
// Print ASCII values of lowercase alphabets (a to z)
for (char c = 'a'; c <= 'z'; c++) {
int asciiValue = (int) c;
System.out.println("ASCII value of " + c + " is " + asciiValue);
}
// Print ASCII values of uppercase alphabets (A to Z)
for (char c = 'A'; c <= 'Z'; c++) {
int asciiValue = (int) c;
System.out.println("ASCII value of " + c + " is " + asciiValue);
}
}
}
Output
ASCII value of a is 97
ASCII value of b is 98
ASCII value of c is 99
ASCII value of d is 100
ASCII value of e is 101
ASCII value of f is 102
ASCII value of g is 103
ASCII value of h is 104
ASCII value of i is 105
ASCII value of j is 106
ASCII value of k is 107
ASCII value of l is 108
ASCII value of m is 109
ASCII value of n is 110
ASCII value of o is 111
ASCII value of p is 112
ASCII value of q is 113
ASCII value of r is 114
ASCII value of s is 115
ASCII value of t is 116
ASCII value of u is 117
ASCII value of v is 118
ASCII value of w is 119
ASCII value of x is 120
ASCII value of y is 121
ASCII value of z is 122
ASCII value of A is 65
ASCII value of B is 66
ASCII value of C is 67
ASCII value of D is 68
ASCII value of E is 69
ASCII value of F is 70
ASCII value of G is 71
ASCII value of H is 72
ASCII value of I is 73
ASCII value of J is 74
ASCII value of K is 75
ASCII value of L is 76
ASCII value of M is 77
ASCII value of N is 78
ASCII value of O is 79
ASCII value of P is 80
ASCII value of Q is 81
ASCII value of R is 82
ASCII value of S is 83
ASCII value of T is 84
ASCII value of U is 85
ASCII value of V is 86
ASCII value of W is 87
ASCII value of X is 88
ASCII value of Y is 89
ASCII value of Z is 90
Explanation
This code will loop through the lowercase and uppercase alphabets, convert each character to its corresponding ASCII value using type casting, and then print the results. When you run the program, you'll see the ASCII values of all the alphabets from 'a' to 'z' and 'A' to 'Z' displayed in the console.
Learn Next: