string to ascii value java code example
Example 1: how to convert an ascii number to character in java
char character = 'a';
int ascii = (int) character;
int ascii = 65;
char character = (char) ascii;
Example 2: how to change a character in a string in java with ascii
import java.text.ParseException;
import java.util.Arrays;
public class StringToASCII {
public static void main(String args[]) throws ParseException {
char A = 'A';
int ascii = A;
System.out.println("ASCII value of 'A' is : " + ascii);
char a = 'a';
int value = (int) a;
System.out.println("ASCII value of 'a' is : " + value);
try {
String text = "ABCDEFGHIJKLMNOP";
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 following
[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
Read more: https://javarevisited.blogspot.com/2015/07/how-to-convert-string-or-char-to-ascii-example.html#ixzz6k2vn7o4y
Example 3: how to change a character in a string in java with ascii
char character = 'a';
int ascii = (int) character;
char character = name.charAt(0);
int ascii = (int) character;
int ascii = character;
Example 4: convert int to ascii java
int yourInt = 33;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);