How To Convert String Or Char To Ascii Values Inward Java

Advertisement

Masukkan script iklan 970x90px

How To Convert String Or Char To Ascii Values Inward Java

Senin, 22 Februari 2021

You tin 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, merely 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 ask an integer variable to shop ASCII values, byte information type inwards Java, which is 8 bits broad is plenty to shop ASCII value of whatsoever character.  So you lot tin too create similar this :
byte asciiOfB = 'B'; // assign 66 to variable

Since String is nil but a grapheme array inwards Java, you lot tin 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 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 top grapheme encoding every bit "US-ASCII" also, every bit nosotros receive got done inwards our Java example, but using StandardCharsets.US_ASCII is safer because in that place is no peril of whatsoever spelling error causing UnsupportedEncodingException. See Core Java Volume 1 tenth Edition past times Cay S. Horstmann to larn to a greater extent than well-nigh String inwards Java.

 past times precisely storing it into a numeric information type e How to convert String or char to ASCII values inwards 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 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 precisely about of the pop encoding formats internally supported past times Java.




See shape java.nio.charset.Charset  together with StandardCharsets for to a greater extent than information

Here is ASCII tabular array for your quick reference :
 past times precisely storing it into a numeric information type e How to convert String or char to ASCII values inwards Java


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 grapheme to ASCII value inwards Java         char A = 'A';         int ascii = A;         System.out.println("ASCII value of 'A' is  : " + ascii);                  // you lot tin 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 fight 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 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 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 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)