why is the output of "y" 55? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why is the output of "y" 55?

static void Main(string[] args) { string[] num = new string[3]{"54230","677809","90132"}; for(int i=0;i<3;i++){ string a = num[1]; int y= Convert.ToInt32(a[2]); Console.WriteLine(a[2]); Console.WriteLine(y);} }

23rd Jun 2022, 4:56 AM
Andrey
4 Answers
+ 2
Andrey a[2] = 7 When you convert "7" to integer then you get ASCII value which is 55
23rd Jun 2022, 6:13 AM
A͢J
A͢J - avatar
+ 2
Andrey 1 - as a[2] gives "7" which is consider as a character so to get 7 as an integer you have to make it string then convert to int like this: int y= Convert.ToInt32(a[2] + ""); //this will give 7 2 - Use Int16.Parse method int y = Int16.Parse(a[2] + ""); a[2] is a character so I make it String because this method except string.
24th Jun 2022, 3:33 AM
A͢J
A͢J - avatar
+ 1
ooh ok, Thank you very much AJ.
24th Jun 2022, 5:50 AM
Andrey
0
And what should I do to make it 7?
23rd Jun 2022, 11:11 PM
Andrey