Getting the ASCII value of a character

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Method

 1 Console.WriteLine("please enter a word:");
 2 string word = Console.ReadLine();
 3 		
 4 foreach(char c in word)
 5 {
 6         int ascii = Convert.ToInt32(c);
 7         Console.WriteLine(c + " in ASCII is " + ascii);
 8 }
 9 
10 Console.ReadLine();

Line 1 & 2 will prompt for the word and store it in a variable called word.

Line 4 is a for loop which will cycle through each character in the word in turn. The current character will be called `c`.

Now you have a `char` you can just convert it to an integer to get the ascii value.