1// Very simple. Just cast your char as an int.
2
3char character = 'a';
4int ascii = (int) character;
5
6//In your case, you need to get the specific Character from the String first and then cast it.
7
8char character = name.charAt(0); // This gives the character 'a'
9int ascii = (int) character; // ascii is now 97.
10
11//Though cast is not required explicitly, but its improves readability.
12
13int ascii = character; // Even this will do the trick.
1import java.text.ParseException;
2import java.util.Arrays;
3
4/**
5 * How to convert a String to ASCII bytes in Java
6 *
7 * @author WINDOWS 8
8 */
9
10public class StringToASCII {
11
12 public static void main(String args[]) throws ParseException {
13
14 // converting character to ASCII value in Java
15 char A = 'A';
16 int ascii = A;
17 System.out.println("ASCII value of 'A' is : " + ascii);
18
19 // you can explicitly cast also
20 char a = 'a';
21 int value = (int) a;
22 System.out.println("ASCII value of 'a' is : " + value);
23
24
25
26
27 // converting String to ASCII value in Java
28 try {
29 String text = "ABCDEFGHIJKLMNOP";
30
31 // translating text String to 7 bit ASCII encoding
32 byte[] bytes = text.getBytes("US-ASCII");
33
34 System.out.println("ASCII value of " + text + " is following");
35 System.out.println(Arrays.toString(bytes));
36
37 } catch (java.io.UnsupportedEncodingException e) {
38 e.printStackTrace();
39 }
40 }
41
42}
43
44Output
45ASCII value of 'A' is : 65
46ASCII value of 'a' is : 97
47ASCII value of ABCDEFGHIJKLMNOP is following
48[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
49
50
51Read more: https://javarevisited.blogspot.com/2015/07/how-to-convert-string-or-char-to-ascii-example.html#ixzz6k2vn7o4y
1char character = name.charAt(0); // This gives the character 'a'
2int ascii = (int) character; // ascii is now 97.