How To Convert String Or Char To Ascii Values Inwards Java
Friday, August 24, 2018
Add Comment
You tin laissez passer on the sack convert a graphic symbol e.g. 'A' to its corresponding ASCII value 65 past times exactly storing it into a numeric information type e.g. byte, int or long every bit shown below :
int asciiOfA = (int) 'A';
Here casting is non necessary, only assigning a graphic symbol to integer is plenty to shop ASCII value of graphic symbol into an int variable, but casting improves readability. Since ASCII is 7-bit graphic symbol encoding, you lot don't fifty-fifty bespeak an integer variable to shop ASCII values, byte information type inwards Java, which is 8 bits broad is plenty to shop ASCII value of whatever character. So you lot tin laissez passer on the sack too produce similar this :
Since String is zilch but a graphic symbol array inwards Java, you lot tin laissez passer on the sack too purpose this technique to convert a String into ASCII values, every bit shown below :
You tin laissez passer on the sack too direct convert String to byte array, where bytes volition concur ASCII value of characters every bit shown below :
You tin laissez passer on the sack operate past times graphic symbol encoding every bit "US-ASCII" also, every bit nosotros receive got done inwards our Java example, but using StandardCharsets.US_ASCII is safer because at that topographic point is no gamble of whatever spelling error causing UnsupportedEncodingException. See Core Java Volume 1 tenth Edition past times Cay S. Horstmann to larn to a greater extent than almost String inwards Java.
See cast java.nio.charset.Charset as well as StandardCharsets for to a greater extent than information
Here is ASCII tabular array for your quick reference :
Sumber https://javarevisited.blogspot.com/
int asciiOfA = (int) 'A';
Here casting is non necessary, only assigning a graphic symbol to integer is plenty to shop ASCII value of graphic symbol into an int variable, but casting improves readability. Since ASCII is 7-bit graphic symbol encoding, you lot don't fifty-fifty bespeak an integer variable to shop ASCII values, byte information type inwards Java, which is 8 bits broad is plenty to shop ASCII value of whatever character. So you lot tin laissez passer on the sack too produce similar this :
byte asciiOfB = 'B'; // assign 66 to variable
Since String is zilch but a graphic symbol array inwards Java, you lot tin laissez passer on the sack too purpose this technique to convert a String into ASCII values, every bit shown below :
StringBuilder sb = novel StringBuilder(); char[] letters = str.toCharArray(); for (char ch : letters) { sb.append((byte) ch); } System.out.println(sb.toString()); // impress 749711897
You tin laissez passer on the sack too direct convert String to byte array, where bytes volition concur ASCII value of characters every bit shown below :
byte[] ascii = "Java".getBytes(StandardCharsets.US_ASCII); String asciiString = Arrays.toString(ascii); System.out.println(asciiString); // impress [74, 97, 118, 97]
You tin laissez passer on the sack operate past times graphic symbol encoding every bit "US-ASCII" also, every bit nosotros receive got done inwards our Java example, but using StandardCharsets.US_ASCII is safer because at that topographic point is no gamble of whatever spelling error causing UnsupportedEncodingException. See Core Java Volume 1 tenth Edition past times Cay S. Horstmann to larn to a greater extent than almost String inwards Java.
Java Program to convert String as well as char to ASCII
Here is our Java program, which combines all the ways nosotros receive got seen to convert String as well as graphic symbol to their respective ASCII values. You tin laissez passer on the sack too purpose 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 to a greater extent than or less of the pop encoding formats internally supported past times Java.See cast java.nio.charset.Charset as well as StandardCharsets for to a greater extent than information
Here is ASCII tabular array for your quick reference :
import java.text.ParseException; import java.util.Arrays; /** * How to convert a String to ASCII bytes inwards Java * * @author WINDOWS 8 */ public class StringToASCII { public static void main(String args[]) throws ParseException { // converting graphic symbol to ASCII value inwards Java char A = 'A'; int ascii = A; System.out.println("ASCII value of 'A' is : " + ascii); // you lot tin laissez passer on the sack explicitly cast also char a = 'a'; int value = (int) a; System.out.println("ASCII value of 'a' is : " + value); // converting String to ASCII value inwards Java try { String text = "ABCDEFGHIJKLMNOP"; // translating text String to vii fleck 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 next [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
That's all guys, forthwith you lot know how to convert a Java char or String to their ASCII values. Remember, when you lot shop a char information type into numeric types e.g. byte, short, int or long, their ASCII values are stored. It's too efficient to purpose byte information type to shop ASCII values because it's a 7-bit graphic symbol encoding as well as byte is plenty to shop ASCII.
Further Learning
Data Structures as well as Algorithms: Deep Dive Using Java
solution]How to convert String to int inwards Java? [solution] How to convert float to String inwards Java? [example] How to convert Double to String inwards Java? [solution] How to convert String to Date inwards a thread-safe manner? [example] How to convert byte array to Hex String inwards Java? [solution] How to convert Decimal to Binary inwards Java? [solution] How to convert String to Integer inwards Java? [solution] How to convert ByteBuffer to String inwards Java? (program)
Further Learning
Data Structures as well as Algorithms: Deep Dive Using Java
solution]
0 Response to "How To Convert String Or Char To Ascii Values Inwards Java"
Post a Comment