convert char array to int array c#

后端 未结 6 999
抹茶落季
抹茶落季 2021-01-01 22:45

I have this array

char[] A = [\'1\', \'2\', \'3\', \'4\']

And I want to convert it to int[]

int[] Aint=[1, 2, 3, 4]
         


        
6条回答
  •  耶瑟儿~
    2021-01-01 22:59

    To get the numeric value of a digit character ('0' to '9'), you can simply subtract the codepoint of '0' from its own.

    int[] Aint = A.Select(a => a - '0').ToArray();
    

    The digit characters are assigned consecutive codepoints. '0' has codepoint 48; '1' has codepoint 49; and so on until '9', which has codepoint 57. Thus, when you subtract two digit characters, you would get the same result as if you were subtracting their numeric values. Subtracting '0' from any digit would give you the latter's absolute value.

提交回复
热议问题