if I assing a string with
string = "12";
and check its value with
Console.Write(string[0] string[1]);
it returns 12 as intended.
but if I assign string[0] to an int array via
int[0] = string[0];
and check its value via
Console.Write(int[0]);
it returns 49.
even if I used Convert.ToInt32()
while assigning, it still returned 49.
Can you help me out here?
CodePudding user response:
You are casting the char '1' into an int which works. Which according to the ASCII table is 49.
For example assigning 'A' to int[0] would set its value to 65.
CodePudding user response:
It is returning 49 because you are trying to convert the value 1
to integer and the ASCII value of 1
is 49. if you want to print the value as 1
to need to use string only, then you can use the code similar to this int.Parse(stringValue);
or you can use TryParse
method as well.
CodePudding user response:
You must just use int.Parse().