How To Convert String Or Char To Ascii Values Inwards Java
You tin post away convert a grapheme e.g. 'A' to its corresponding ASCII value 65 past times precisely 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 grapheme to integer is plenty to shop ASCII value of grapheme into an int variable, but casting improves readability. Since ASCII is 7-bit grapheme encoding, y'all don't fifty-fifty postulate an integer variable to shop ASCII values, byte information type inward Java, which is 8 bits broad is plenty to shop ASCII value of whatever character. So y'all tin post away likewise produce similar this :
Since String is naught but a grapheme array inward Java, y'all tin post away likewise purpose this technique to convert a String into ASCII values, every bit shown below :
You tin post away likewise direct convert String to byte array, where bytes volition concur ASCII value of characters every bit shown below :
You tin post away transcend grapheme encoding every bit "US-ASCII" also, every bit nosotros receive got done inward our Java example, but using StandardCharsets.US_ASCII is safer because in that place is no direct chances 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 near String inward Java.
See course of education java.nio.charset.Charset as well as StandardCharsets for to a greater extent than information
Here is ASCII tabular array for your quick reference :
int asciiOfA = (int) 'A';
Here casting is non necessary, only assigning a grapheme to integer is plenty to shop ASCII value of grapheme into an int variable, but casting improves readability. Since ASCII is 7-bit grapheme encoding, y'all don't fifty-fifty postulate an integer variable to shop ASCII values, byte information type inward Java, which is 8 bits broad is plenty to shop ASCII value of whatever character. So y'all tin post away likewise produce similar this :
byte asciiOfB = 'B'; // assign 66 to variable
Since String is naught but a grapheme array inward Java, y'all tin post away likewise 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 post away likewise 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 post away transcend grapheme encoding every bit "US-ASCII" also, every bit nosotros receive got done inward our Java example, but using StandardCharsets.US_ASCII is safer because in that place is no direct chances 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 near String inward 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 grapheme to their respective ASCII values. You tin post away likewise 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 precisely about of the pop encoding formats internally supported past times Java.See course of education 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 inward Java * * @author WINDOWS 8 */ public class StringToASCII { public static void main(String args[]) throws ParseException { // converting grapheme to ASCII value inward Java char A = 'A'; int ascii = A; System.out.println("ASCII value of 'A' is : " + ascii); // y'all tin post away explicitly cast also char a = 'a'; int value = (int) a; System.out.println("ASCII value of 'a' is : " + value); // converting String to ASCII value inward Java try { String text = "ABCDEFGHIJKLMNOP"; // translating text String to vii flake 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, straight off y'all know how to convert a Java char or String to their ASCII values. Remember, when y'all shop a char information type into numeric types e.g. byte, short, int or long, their ASCII values are stored. It's likewise efficient to purpose byte information type to shop ASCII values because it's a 7-bit grapheme 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 inward Java? [solution] How to convert float to String inward Java? [example] How to convert Double to String inward Java? [solution] How to convert String to Date inward a thread-safe manner? [example] How to convert byte array to Hex String inward Java? [solution] How to convert Decimal to Binary inward Java? [solution] How to convert String to Integer inward Java? [solution] How to convert ByteBuffer to String inward Java? (program)
Further Learning
Data Structures as well as Algorithms: Deep Dive Using Java
solution]


Komentar
Posting Komentar