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"?

    1 answer 1

    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 .

    • Flammable: that is, Convert.ToInt32 ('a') == 52 Convert.ToInt32 ("a") == 4? Funny did not know. - VladD
    • @VladD don't believe him, he is joking. Or are you kidding? The second line will give an error. - alexlz
    • one
      @alexlz: Oh, typo, sorry. Convert.ToInt32 ('4') == 52 Convert.ToInt32 ("4") == 4 (< ideone.com/x2brzL> ) - VladD