Getting the ASCII value of a character

From TRCCompSci - AQA Computer Science
Revision as of 10:41, 11 December 2023 by Admin (talk | contribs) (Created page with "==Method 1== <syntaxhighlight lang=csharp line> Console.WriteLine("please enter a word:"); string word = Console.ReadLine(); foreach(char c in word) { int ascii = C...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Method 1

 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.