16

I have this array

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

And I want to convert it to int[]

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

Any ideas?

I just started programming

Thanks

1
  • 3
    What attempts have you made to solve this problem yourself? What problems are you having with those attempts? Do you know how to convert one char to it's corresponding int value? Do you know how to create a new array based on another array's values? Commented Feb 5, 2014 at 20:00

6 Answers 6

38

Another option, using Array.ConvertAll and Char.GetNumericValue:

int[] Aint = Array.ConvertAll(A, c => (int)Char.GetNumericValue(c));
Sign up to request clarification or add additional context in comments.

11 Comments

I think this is the best solution so far because it is extremely easier to read and understand at a glance. It's practically written in plain English.
Is there any reason why you used Array.ConvertAll instead of Select(…).ToArray()? I'm asking because the latter seems to be much more in vogue nowadays (and your lambda expression requires C# 3 anyway).
@Douglas: Array.ConvertAll is just more efficient than Select..ToArray since it pre-allocates a correctly-sized array and doesn't do any resizing. It's also readable/explicit as Gray has already mentioned.
@Douglas: Also, it works already with .NET 2 even with the lambda.
@TimSchmelter: Yes, I agree it's more efficient in that respect. By mentioning C# 3, I meant that it requires the same version of C# (and .NET) as LINQ would.
|
11

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.

2 Comments

You need to have the System.Linq namespace included in your code because of the use of the Select method. You can do this by adding using System.Linq to the top of your code.
@Krige: You're right. Visual Studio 2013 includes it automatically for you when you create a new class file.
6

Add a using statement for using System.Linq; then you can do the following:

int[] Aint = A.Select(i => Int32.Parse(i.ToString())).ToArray();

You will get an exception if an element in A cannot be parsed.

2 Comments

Concise is usually good but not so much for someone who has "just started programming".
Well he didn't ask for a specific implementation (for loop, while loop, etc) and I like Linq so that's how I implemented it.
4

Please do Like this

  char[] A = {'1', '2', '3', '4'};
  int[] Aint = new int[A.Length];      
  for (int i = 0;i < A.Length;i++)
  {
      Aint[i] = Convert.ToInt32(A[i].ToString()); 
  }

1 Comment

Convert.ToInt32, small mistake in solution.
2

A little bit of Linq should do the trick:

int[] Aint = A.Select(c => c - 48).ToArray(); // or c - '0'

Comments

0

You can also do this:

int[] Aint = A.Select(c => Convert.ToInt32(c.ToString())).ToArray();

This will select all character array as string, converts them to array, then return an integer of array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.