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

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(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...")
 
(Method 1)
 
(3 intermediate revisions by the same user not shown)
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:");
Line 7: Line 7:
 
{
 
{
 
         int ascii = Convert.ToInt32(c);
 
         int ascii = Convert.ToInt32(c);
Console.WriteLine(c + " in ASCII is " + ascii);
+
        Console.WriteLine(c + " in ASCII is " + ascii);
 
}
 
}
  

Latest revision as of 10: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.