Difference between revisions of "Getting the ASCII value of a character"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Undo revision 8754 by Admin (talk))
(Tag: Undo)
(Method 1)
 
Line 1: Line 1:
==Method 1==
+
==Method==
 
<syntaxhighlight lang=csharp line>
 
<syntaxhighlight lang=csharp line>
 
Console.WriteLine("please enter a word:");
 
Console.WriteLine("please enter a word:");

Latest revision as of 11:43, 11 December 2023

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.