Char to ASCII and ASCII to char conversion in Java
This post will discuss how to convert a char to its ASCII value, and vice versa, in Java.
1. Using Casting or Implicit Conversion
A simple way to convert a char to its ASCII value is to cast the char as an int, which will return the ASCII value of the char. Casting is an explicit conversion from one data type to another. Here’s a Java program that demonstrates this:
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) { char ch = 'A'; int ascii = (int) ch; System.out.println(ascii); // 65 } } |
Note that casting char to int is redundant, although it improves readability. We can take advantage of the implicit conversion by the compiler when a character value is assigned to an integer. This implicit unboxing works since the compiler can determine the result of this expression. For example:
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) { char ch = 'A'; int ascii = ch; System.out.println(ascii); // 65 } } |
To do the opposite, i.e., get the corresponding char from the ASCII value in Java, we can simply cast it into a char. This will not result in any loss of precision if the integer represents the ASCII range of characters.
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) { int ascii = 65; char ch = (char) ascii; System.out.println(ch); // 'A' } } |
2. Using codePointAt()
method
Alternatively, we can use the codePointAt(int)
method of the String
class to get the ASCII value of a char in a string. Here is an example of using this method for a single char:
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { char ch = 'A'; int ascii = String.valueOf(ch).codePointAt(0); System.out.println(ascii); // 65 } } |
The Character
class also provides the static method codePointAt(char[], int)
, which can obtain the ASCII value of a char as follows:
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { char ch = 'A'; int ascii = Character.codePointAt(new char[]{ch}, 0); System.out.println(ascii); // 65 } } |
To get the corresponding character from the ASCII value in Java, we can use the Character.toChars(int)
method. This method converts the specified char to its UTF-16 representation and returns a char array. For example:
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) { int ascii = 65; char ch = Character.toChars(ascii)[0]; System.out.println(ch); // 'A' } } |
3. Using getBytes()
method
If we need to convert each character of a string to ASCII, we can encode the string into a sequence of bytes using the getBytes()
method of the String
class, which results in a new byte array. To get the ASCII value for a single char, we can convert the char to a string first using the String.valueOf(char)
method, then call the getBytes()
method on the string, and access the first index like below:
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { char ch = 'A'; byte[] bytes = String.valueOf(ch).getBytes(); int ascii = bytes[0]; System.out.println(ascii); // 65 } } |
That’s all about converting a char to its ASCII value, and vice versa, in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)