How To Convert String Or Char To Ascii Values Inward Java
You tin post away convert a grapheme 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 equally shown below :
int asciiOfA = (int) 'A';
Here casting is non necessary, exactly 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, you lot don't fifty-fifty necessitate 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 you lot tin post away likewise produce similar this :
Since String is zilch but a grapheme array inward Java, you lot tin post away likewise purpose this technique to convert a String into ASCII values, equally shown below :
You tin post away likewise straight convert String to byte array, where bytes volition concur ASCII value of characters equally shown below :
You tin post away run past times grapheme encoding equally "US-ASCII" also, equally nosotros direct maintain 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 virtually String inward Java.
See course of pedagogy java.nio.charset.Charset together with 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, exactly 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, you lot don't fifty-fifty necessitate 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 you lot tin post away likewise produce similar this :
byte asciiOfB = 'B'; // assign 66 to variable
Since String is zilch but a grapheme array inward Java, you lot tin post away likewise purpose this technique to convert a String into ASCII values, equally 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 straight convert String to byte array, where bytes volition concur ASCII value of characters equally 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 run past times grapheme encoding equally "US-ASCII" also, equally nosotros direct maintain 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 virtually String inward Java.
Java Program to convert String together with char to ASCII
Here is our Java program, which combines all the ways nosotros direct maintain seen to convert String together with 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 about of the pop encoding formats internally supported past times Java.See course of pedagogy java.nio.charset.Charset together with 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); // you lot 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 chip 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 likewise efficient to purpose byte information type to shop ASCII values because it's a 7-bit grapheme encoding together with byte is plenty to shop ASCII.
Further Learning
Data Structures together with 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 together with Algorithms: Deep Dive Using Java
solution]
0 Response to "How To Convert String Or Char To Ascii Values Inward Java"
Post a Comment