Convert an array of chars to an array of integers

I have these arrays

char[] array = {'1', '2', '3', '4'};
int[] sequence = new int[array.Length];  

Is there an easy way to assign the numbers in array to sequence?

I tried this

for (int i = 0; i < array.Length; i++)
{
    seqence[i] = Convert.ToInt32(array[i]);
}

But I get the ASCII coding of 1, 2, 3, 4 not the numbers by itself.

Jon Skeet
people
quotationmark

If you convert each character to a string first, then use int.Parse (or still Convert.ToInt32) that will work.

Personally I'd use LINQ for this, e.g.

int[] sequence = array.Select(x => int.Parse(x.ToString())).ToArray();

... or use ToList if you're just as happy with List<int>.

If you want to use Char.GetNumericValue as suggested in another answer, you can use that with LINQ too:

int[] sequence = array.Select(x => (int) char.GetNumericValue(x)).ToArray();

Note that the cast to int is required because char.GetNumericValue returns a double. (There are Unicode characters for values such as "a half" for example.)

Or if you're absolutely sure you're just going to have ASCII digits, you could use the quick and dirty:

int[] sequence = array.Select(x => x - '0').ToArray();

people

See more on this question at Stackoverflow