Question:
How can I tell what the ASCII code is for each character in a string?
Answer:
By casting each character to an int.
Code:
public class StringASCII { public static void main(String[] args){ String str = "abc ABC 123"; for(int i=0; i< str.length() ;i++){ System.out.print(str.charAt(i)); System.out.print(" : "); System.out.println((int)str.charAt(i)); } } }
Output:
$ java StringASCII a : 97 b : 98 c : 99 : 32 A : 65 B : 66 C : 67 : 32 1 : 49 2 : 50 3 : 51