ASCII AND MORE ON CHAR Lesson 13 What is ASCII? Watch this video for an intro to ASCII. ASCII Legal operations with strings, integers, and characters ASCII "ask-key"...means the letter "A" is stored as a number "65" letter "Z" is stored as a "90". you need to memorize what the ASCII for A and Z are, but not the ones in between. Also memorize "a" which is "97" and "z" which is "122" Also the character "0" is a "48" and "9" is a "57" Look at Appendix D in your book for a complete reference. Watch This Video ASCII and more on Char ASCII codes Let’s look at some things you can and can’t do with char and Strings Generally you can’t store a.. Char in a String String in a Char char a = aString; //illegal char b =”A”; //illegal char b = ‘A’; //legal String s = xChar; //illegal even if previous declared as a char String s = ‘X’; //illegal even if a literal char like X So, what can you do? int x = 1; char a = ‘A’; //ASCII code for a is what? 65 int y = x + a; //Can you do this? It’s 2 different types? // int and char. // Remember a char is very close to a int System.out.print(y); //66 int z = a System.out.println(z); Now let’s discuss something else here. { public static void main(String args[]) { int x = 1; char ch = x; //Will this work? Why or why not? // What if we change it x to 65? //Why won't it work? //It doesn't know at the time of compiling what the value of x. //Integers can go all the way up to a value of over 2 billion. //Char can go all the way up to 65,000 } } { int x = 65; char ch = (char)x; //Now there is a way to force it. (char) //If x was greater than 65,000 then it would compile but crash at run time. System.out.println(ch); What will it print? } String char conversions, the Character class Early we stated you can’t store a char into a String and vise versa. But suppose you really have to. Well let’s look at a technique at doing this. string s = “W”; char a = s; //this won’t work string s = “W”; char a = s.charAt(0); //look at your index, so this one has an index of 0 This one will comple. Let’s go the other way. char a = ‘X’; String s = a; //this won’t work So how do we get it to work? char a = ‘X’; String s = “” + a; // “” is an empty string 2 quotes with nothing in between. Now concatenate onto this String the char // This will compile Let’s look at the book for a second, look at page 2 of chapter 13. What are you? (just ask) To understand this we are going to build a char each time (ch) Remember the Math. Class Character.Class…it has methods The method Digit returns a boolean. Is it a digit or not? Letter? Letter OrDigit? Whitespace? //check your index to see what this means. //a blank, space, tab ..etc LowerCase? UpperCase? Upper/lower case conversions Let’s go ahead and take a look how we would convert a upper case character to lower case and vice versa. char ch = ‘d’; //lower case d stored in it. char x = Character.toUpperCase(ch); System.out.println(x) char ch = ‘D’; //lower case d stored in it. char x = Character.toLowerCase(ch); System.out.println(x)