You can convert a character like 'A' to its corresponding ASCII value 65 by just storing it into a numeric data type like byte, int, or long as shown below :
int asciiOfA = (int) 'A';
Here casting is not necessary, simply assigning a character to an integer is enough to store the ASCII value of character into an int variable, but casting improves readability. Since ASCII is a 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 the ASCII value of any character. So you can also do like this :
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 :
You can also directly convert String to a byte array, where bytes will hold ASCII value of characters as shown below :
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. See Core Java Volume 1 10th Edition by Cay S. Horstmann to learn more about String in Java.
See class java.nio.charset.Charset and StandardCharsets for more information
Here is an ASCII table for your quick reference :
int asciiOfA = (int) 'A';
Here casting is not necessary, simply assigning a character to an integer is enough to store the ASCII value of character into an int variable, but casting improves readability. Since ASCII is a 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 the 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 a 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. See Core Java Volume 1 10th Edition by Cay S. Horstmann to learn more about String in Java.
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.See class java.nio.charset.Charset and StandardCharsets for more information
Here is an ASCII table for your quick reference :
And, here is our complete Java program to convert a given String to ASCII values in Java:
Other data type conversion tutorials from this blog :
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.
Other data type conversion tutorials from this blog :
- How to convert Char to String in Java? [solution]
- How to convert String to int in Java? [solution]
- How to convert float to String in Java? [example]
- How to convert Double to String in Java? [solution]
- How to convert String to Date in a thread-safe manner? [example]
- How to convert byte array to Hex String in Java? [solution]
- How to convert Decimal to Binary in Java? [solution]
- How to convert String to Integer in Java? [solution]
- How to convert ByteBuffer to String in Java? (program)
What is the complexity of this progrogram?
ReplyDeletei want java full code to convert plain text into ascii and vice versa.
ReplyDeleteGreat
ReplyDeleteThanks @Unknown
ReplyDelete