How To Convert String Or Char To Ascii Values Inwards Java
You tin sack convert a grapheme e.g. 'A' to its corresponding ASCII value 65 yesteryear precisely 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, but 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 require an integer variable to shop ASCII values, byte information type inward Java, which is 8 bits broad is plenty to shop ASCII value of whatsoever character. So you lot tin sack equally good produce similar this :
Since String is null but a grapheme array inward Java, you lot tin sack equally good piece of job this technique to convert a String into ASCII values, equally shown below :
You tin sack equally good straight convert String to byte array, where bytes volition agree ASCII value of characters equally shown below :
You tin sack overstep grapheme encoding equally "US-ASCII" also, equally nosotros receive got done inward our Java example, but using StandardCharsets.US_ASCII is safer because at that spot is no guide chances of whatsoever spelling error causing UnsupportedEncodingException. See Core Java Volume 1 tenth Edition yesteryear Cay S. Horstmann to larn to a greater extent than most String inward Java.
See course of didactics 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, but 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 require an integer variable to shop ASCII values, byte information type inward Java, which is 8 bits broad is plenty to shop ASCII value of whatsoever character. So you lot tin sack equally good produce similar this :
byte asciiOfB = 'B'; // assign 66 to variable
Since String is null but a grapheme array inward Java, you lot tin sack equally good piece of job 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 sack equally good straight convert String to byte array, where bytes volition agree 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 sack overstep grapheme encoding equally "US-ASCII" also, equally nosotros receive got done inward our Java example, but using StandardCharsets.US_ASCII is safer because at that spot is no guide chances of whatsoever spelling error causing UnsupportedEncodingException. See Core Java Volume 1 tenth Edition yesteryear Cay S. Horstmann to larn to a greater extent than most String inward Java.
Java Program to convert String together with char to ASCII
Here is our Java program, which combines all the ways nosotros receive got seen to convert String together with grapheme to their respective ASCII values. You tin sack equally good piece of job 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 around of the pop encoding formats internally supported yesteryear Java.See course of didactics 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 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 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, right away 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 equally good efficient to piece of job 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]
Belum ada Komentar untuk "How To Convert String Or Char To Ascii Values Inwards Java"
Posting Komentar