Why does Convert.ToInt32('1') returns 49?
It is returning the ASCII value of character 1
The first statement treats the argument as string and converts the value into Int, The second one treats the argument as char and returns its ascii value
The code '1'
is the same as (char)49
(because the Unicode code point of the character 1
is 49). And Convert.ToInt32(char)
returns the code point of that character as an int
.
As the others said, Convert returns the ASCII code.
If you want to convert '1'
to 1 (int)
you should use
int val = Convert.ToInt32('1'.ToString());