Convert char to int in C#

后端 未结 14 812
情深已故
情深已故 2020-11-22 10:44

I have a char in c#:

char foo = \'2\';

Now I want to get the 2 into an int. I find that Convert.ToInt32 returns the actual decimal value o

14条回答
  •  抹茶落季
    2020-11-22 11:11

    Comparison of some of the methods based on the result when the character is not an ASCII digit:

    char c1 = (char)('0' - 1), c2 = (char)('9' + 1); 
    
    Debug.Print($"{c1 & 15}, {c2 & 15}");                                   // 15, 10
    Debug.Print($"{c1 ^ '0'}, {c2 ^ '0'}");                                 // 31, 10
    Debug.Print($"{c1 - '0'}, {c2 - '0'}");                                 // -1, 10
    Debug.Print($"{(uint)c1 - '0'}, {(uint)c2 - '0'}");                     // 4294967295, 10
    Debug.Print($"{char.GetNumericValue(c1)}, {char.GetNumericValue(c2)}"); // -1, -1
    

提交回复
热议问题