. Advertisement .
..3..
. Advertisement .
..4..
This tutorial shall inform you how to locate and convert ASCII to char Java. As such, employing casting, character.toString
, and character.toChars()
methods are the most accessible approaches used to accomplish this.
How To Convert ASCII To Char Java
Method #1: Employing Character.toString Function(Convert To Upper-Case Alphabets)
The Java character class offers us a toString()
function, which is also one of the feasible ways to convert a codePoint to a char. In this scenario, that codePoint is no further to seek than an ASCII code.
To obtain all the uppercase English alphabets, we can loop the conversion technique. Keep in mind that the loop ranges from 65 to 90, which are uppercase alphabet-corresponding codes.
The advantage of this technique over others is that it has the ability to throw an exception if the int value is not verified properly.
Running the code:
public class Main {
public static void main(String[] args) {
int asciiValue = 65;
for(int i = asciiValue; i <= 90; i++){
String convertedChar = Character.toString(i);
System.out.println(i+" => "+convertedChar);
}
}
}
Output:
65 => A
66 => B
67 => C
68 => D
69 => E
70 => F
71 => G
72 => H
73 => I
74 => J
75 => K
76 => L
77 => M
78 => N
79 => O
80 => P
81 => Q
82 => R
83 => S
84 => T
85 => U
86 => V
87 => W
88 => X
89 => Y
90 => Z
Method #2: Employing Character.toString Function (Convert To Lower-Case Alphabets)
Similarly, we have the adoption of the toString()
method to convert the ASCII codes into lower-case alphabets. As such, simply alter the loop range and here you go to solve the math. Note that it should now begin with 97
and stop at 122
.
Running the code:
public class Main {
public static void main(String[] args) {
int asciiValue = 97;
for(int i = asciiValue; i <= 122; i++){
String convertedChar = Character.toString((char)i);
System.out.println(i+" => "+convertedChar);
}
}
}
Output:
97 => a
98 => b
99 => c
100 => d
101 => e
102 => f
103 => g
104 => h
105 => i
106 => j
107 => k
108 => l
109 => m
110 => n
111 => o
112 => p
113 => q
114 => r
115 => s
116 => t
117 => u
118 => v
119 => w
120 => x
121 => y
122 => z
Method #3: Utilizing Casting
Here goes another straightforward short-cut for you to get there faster: Casting the ASCII code straight to a char, which will change the asciiValue of the int type to a char type, is the simplest and most basic technique to extract a character from an ASCII code.
Running the code:
public class Main {
public static void main(String[] args) {
int asciiValue = 97;
char convertedChar = (char)asciiValue;
System.out.println(convertedChar);
}
}
Output:
a
Method #4: Employing Character.toChars() Function
We may also utilize the toChars
method of the Java character class, which accepts a codePoint that is similar to the ASCII value and produces an array of char.
Running the code:
public class Main {
public static void main(String[] args) {
int asciiValue = 255;
char[] convertedCharArray = Character.toChars(asciiValue);
System.out.println(convertedCharArray);
}
}
Output:
ÿ
The Bottom Line
This guide on how to convert ASCII to char Java should be able to provide you some useful information.
Now that you have all you wanted to know, let’s go on and try out the programming trial our own way!
And don’t forget to check out our other intriguing articles on several relevant topics. See you then!
Leave a comment