Trying to convert first symbol of string to int, getting weird value

c#, integer, string

Solution

If you use:

Convert.ToInt32(str_val[0]);

then you are actually calling the overload:

Convert.ToInt32(char val);

which gives the Unicode/Ascii number of character being passed as a parameter.

If you want to convert first character, you need to force it to be a string type:

Convert.ToInt32(str_val.Substring(0, 1));

This way you call the overload:

Convert.ToInt32(string val);

which actually do what you want (convert string value to int value that this string represents).

Problem

``` static void Main(string[] args) { string str_val = "8584348,894"; //int prefix = Convert.ToInt32(str_val[0]); //prefix = 56 O_o //int prefix = (int)str_val[0]; //what, again 56? i need 8! int prefix = Convert.ToInt32("8"); //at least this works -_- } ``` Any idea how to convert first symbol to right numeric value?

Original source