In this post, we will see How to convert Character to ASCII Numeric Value in Java.
There are multiple ways to convert Character to ASCII Numeric Value in Java
Table of Contents
By casting char to int
You can simply get char from String using charAt()
and cast it to int.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; public class CharToASCIICast { public static void main(String[] args) { String s="Hello"; char c=s.charAt(1); int asciiOfE=(int)c; System.out.println("Ascii value of e is: "+asciiOfE); } } |
Output:
You can even directly assign char to int, but it is good idea to explicitly cast it for readabiliy.
You can change highlighted code to below line and program will still work:
1 2 3 |
int asciiOfE = c; |
Using toCharArray()
You can simply use index with toCharArray()
to get ASCII value of character in the String.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; public class CharToASCIICast { public static void main(String[] args) { String s="Hello"; char c=s.toCharArray()[1]; int asciiOfE=(int)c; System.out.println("Ascii value of e is: "+asciiOfE); } } |
Output:
Using String’s getBytes()
You can convert String to byte array using getBytes(StandardCharsets.US_ASCII)
and this byte array will contain character’s ASCII values. You can access individual value by accessing byte array by index.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.nio.charset.StandardCharsets; public class CharToASCIIGetBytes { public static void main(String[] args) { String str = "Hello"; byte[] bytes = str.getBytes(StandardCharsets.US_ASCII); System.out.println("Ascii value of e is: "+bytes[1]); System.out.println("ASCII values for all characters are:"); for(byte b:bytes) { System.out.print(b+" "); } } } |
Output:
Using String’s char() [Java 9+]
You can convert String to IntStream using String’s chars()
method, use boxed()
to convert it to Stream of wrapper type Integer
and collect to the list. Result list will contain all the ascii value of the characters and you can use index to access individual ASCII value of character.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import java.util.List; import java.util.stream.Collectors; public class CharToASCIIUsingIntStream { public static void main(String[] args) { String str="Hello"; List<Integer> asciiIntegers = str.chars() .boxed() .collect(Collectors.toList()); System.out.println("ASCII values for all characters are:"); for(int i:asciiIntegers) { System.out.print(i+" "); } } } |
Output:
Convert a String of letters to an int of corresponding ascii
If you want to convert entire String into concatenated ASCII value of int type, you can create StringBuilder
from String’s ASCII values and convert it to BigInteger.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import java.math.BigInteger; public class CharToASCIIInt { public static void main(String[] args) { String str="Hello"; StringBuilder sb = new StringBuilder(); for (char ch : str.toCharArray()) { sb.append((int)ch); } BigInteger biAscii = new BigInteger(sb.toString()); System.out.println(biAscii); } } |
Output:
That’s all about Convert Character to ASCII in Java