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
3 Antworten
+ 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;
.
.
.
.
+ 1
Thank you Rabee for making it clear!
0
Thank you Hasan