Why such a code
string a = "45"; Console.WriteLine(Convert.ToInt32(a));
displays "45" and such
string a = "45"; Console.WriteLine(Convert.ToInt32(a[0]));
displays "52" and not "4"? How to get "4"?
Why such a code
string a = "45"; Console.WriteLine(Convert.ToInt32(a));
displays "45" and such
string a = "45"; Console.WriteLine(Convert.ToInt32(a[0]));
displays "52" and not "4"? How to get "4"?
displays "45"
We read MSDN:
Converts the specified string representation of a number to an equivalent 32-bit signed integer.
...
displays "52" and not "4"?
Read MSDN again:
Converts the value of the specified Unicode character to an equivalent 32-bit signed integer.
Here, the value is the number in the symbol table.
...
How to get "4"?
For example, like this:
Console.WriteLine(Convert.ToInt32(a.Substring(0,1)));
upd. Already discussed on stackoverflow .
Source: https://ru.stackoverflow.com/questions/198595/
All Articles