Friday, July 21, 2017

How to convert String or char to ASCII values in Java

You can convert a character e.g. 'A' to its corresponding ASCII value 65 by just storing it into a numeric data type e.g. byte, int or long as shown below :

int asciiOfA = (int) 'A';

Oracle Java Tutorial and Material, Oracle Java Certification, Oracle Java Learning

Here casting is not necessary, simply assigning a character to integer is enough to store ASCII value of character into an int variable, but casting improves readability. Since ASCII is 7-bit character encoding, you don't even need an integer variable to store ASCII values, byte data type in Java, which is 8 bits wide is enough to store ASCII value of any character.  So you can also do like this :

byte asciiOfB = 'B'; // assign 66 to variable

Since String is nothing but a character array in Java, you can also use this technique to convert a String into ASCII values, as shown below :

StringBuilder sb = new StringBuilder();
char[] letters = str.toCharArray();

for (char ch : letters) {
    sb.append((byte) ch);
}

System.out.println(sb.toString()); // print 749711897

You can also directly convert String to byte array, where bytes will hold ASCII value of  characters as shown below :

byte[] ascii = "Java".getBytes(StandardCharsets.US_ASCII);
String asciiString = Arrays.toString(ascii);
System.out.println(asciiString); // print [74, 97, 118, 97]

You can pass character encoding as "US-ASCII" also, as we have done in our Java example, but using StandardCharsets.US_ASCII is safer because there is no chance of any spelling mistake causing UnsupportedEncodingException.

Java Program to convert String and char to ASCII


Here is our Java program, which combines all the ways we have seen to convert String and character to their respective ASCII values. You can also use the same technique to convert String to other encoding formats e.g. ISO-8859-X (1-7) , UTF-8 , UTF-16BE , UTF-16LE. These are some of the popular encoding formats internally supported by Java.

Here is ASCII table for your quick reference :

Oracle Java Tutorial and Material, Oracle Java Certification, Oracle Java Learning

import java.text.ParseException;
import java.util.Arrays;

/**
 * How to convert a String to ASCII bytes in Java
 * 
 * @author WINDOWS 8
 */

public class StringToASCII {

    public static void main(String args[]) throws ParseException {
        
        // converting character to ASCII value in Java
        char A = 'A';
        int ascii = A;
        System.out.println("ASCII value of 'A' is  : " + ascii);
        
        // you can explicitly cast also
        char a = 'a';
        int value = (int) a;
        System.out.println("ASCII value of 'a' is  : " + value);
        
        
        
        
        // converting String to ASCII value in Java
        try {
            String text = "ABCDEFGHIJKLMNOP";

            // translating text String to 7 bit ASCII encoding
            byte[] bytes = text.getBytes("US-ASCII");
            
            System.out.println("ASCII value of " + text + " is following");
            System.out.println(Arrays.toString(bytes));
            
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}

Output
ASCII value of 'A' is  : 65
ASCII value of 'a' is  : 97
ASCII value of ABCDEFGHIJKLMNOP is following
[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]

That's all guys, now you know how to convert a Java char or String to their ASCII values. Remember, when you store  a char data type into numeric types e.g. byte, short, int or long, their ASCII values are stored. It's also efficient to use byte data type to store ASCII values because it's a 7-bit character encoding and byte is enough to store ASCII.