11

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.

2
  • 1
    Try changing the numbers in the string from '1' to "1" and so on. This should change it so they change to integers. Commented Oct 31, 2013 at 13:46
  • But I get the ASCII coding of 1,2,3,4 not the numbers themselves. Actually, you get the 32-bit signed integer of the Unicode character. Commented Oct 31, 2013 at 13:53

9 Answers 9

11
using System.Linq;

char[] array = { '1', '2', '3', '4' };
var ints = array.Select(x => int.Parse(x.ToString()));
Sign up to request clarification or add additional context in comments.

Comments

7

Just convert the char to a string first:

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

But of course, you could do this all in a single linq query:

char[] array = {'1', '2', '3', '4'};
int[] sequence = array.Select(c => Convert.ToInt32(c.ToString())).ToArray();

Comments

4

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();

Comments

3

try

for (int i = 0; i < arr.Length; i++)
 {
     seqence[i] = (int)Char.GetNumericValue(array[i]);
 }

Comments

1

You can may be use Enumerable.Range to achieve that. It generates a sequence of consecutive numbers from - to.

Just an example:

 IEnumerable<int> squares = Enumerable.Range(1, 10)

This is in the case if original char array is just a consecutive sequence of numbers in char.

If array is subject of arbitrary order and sequence, just use correct convertion:

array.Select(x => int.Parse(x.ToString()))

Comments

0

Use this:

for (int i = 0; i < array.Length; i++)
{
    sequence[i] = array[i] - '0';
}

There are many correct answers here, but (unless you want to use unsafe code) this is the fastest, and it doesn't use additional memory.

Comments

0

Try this:

char[] array = { '1', '2', '3', '4' };

int[] sequence = new int[array.Length];

for (int i = 0; i < array.Length; i++)
{
    sequence[i] = int.Parse(array[i].ToString());
}

Comments

0

Try to convert your Char value to String before converting to Integer:

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

Comments

0

iterate over your char array and use the next conversion:

char[] charArray = { '1', '2', '3', '4' };

int[] intArray= new int[charArray .Length];

for (int i = 0; i < charArray .Length; i++)
{
    intArray[i] = (int)Char.GetNumericValue(charArray[i]);   

}

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.