[SOLVED]Why is converting from string not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[SOLVED]Why is converting from string not working?

The input is '371' and the piece of code is: string x = Console.ReadLine(); int a = Convert.ToInt32(x[0]); Console.WriteLine(a); It then outputs 51, why isn't it 3 as the 1st character of the string?

15th Jul 2018, 9:58 AM
Matus Wall
Matus Wall - avatar
1 Answer
+ 2
You are accessing a character of a string via it's index position. A character is converted to it's ASCII counterpart via ToInt32(). http://www.asciitable.com/mobile/ Instead, you may do: int a = Convert.ToInt32(new string(x[0],1));
15th Jul 2018, 10:24 AM
Hatsy Rei
Hatsy Rei - avatar