0

Converting string into integer?

// why can't I convert each element in a string into each integer in this situation? string num = "7654382"; int length = num.Length; while (x < length) { Console.WriteLine(Convert.ToInt32(num[x])); x++; } //I am trying to find largest or second largest number or lowest number

11th Mar 2018, 4:48 PM
Suraj Sharma
Suraj Sharma - avatar
3 Respuestas
+ 2
Because num[x] returns a char type , for example num[0] returns '7' as a char not "7" as a string , and when converting it to a int it will return the Unicode of that character , in our example when converting '7' to int it will return 55 , to avoid this you can create a method that takes char as an argument and return the corresponding int , for example : static int ConvertCharToInt(char digit) { switch (digital) { case '0': return 0; case '1': return 1; . . . .
11th Mar 2018, 7:31 PM
Rabee Abbas
Rabee Abbas - avatar
+ 1
Thank you Rabee for making it clear!
12th Mar 2018, 8:01 PM
Suraj Sharma
Suraj Sharma - avatar
0
Thank you Hasan
12th Mar 2018, 8:00 PM
Suraj Sharma
Suraj Sharma - avatar