Getting the ASCII value of a character

From TRCCompSci - AQA Computer Science
Revision as of 10:43, 11 December 2023 by Admin (talk | contribs) (Method 1)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.