How to convert int array to int?

What I would like to know how to do is convert an int array to an int in C #.

However, I want to add int with values ​​from the array.

Example:

int[] array = {5, 6, 2, 4};

Will be converted to int equal to 5624.

Thanks for any help in advance.

+5
source share
11 answers

just multiply each number by 10 ^ its place in the array.

int[] array = { 5, 6, 2, 4 };
int finalScore = 0;
for (int i = 0; i < array.Length; i++)
{
    finalScore += array[i] * Convert.ToInt32(Math.Pow(10, array.Length-i-1));
}
+13
source
int output = array
    .Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1)))
    .Sum();
+6
source

:

int[] array =  {5, 6, 2, 4};
int num;
if (Int32.TryParse(string.Join("", array), out num))
{
    //success - handle the number
}
else
{
    //failed - too many digits in the array
}

, .

+4

:

        int[] intArray = new int[] { 5, 4, 6, 1, 6, 8 };

        int total = 0;
        for (int i = 0; i < intArray.Length; i++)
        {
            int index = intArray.Length - i - 1;
            total += ((int)Math.Pow(10, index)) * intArray[i];
        }
+2

, int,

String a;
int output;
int[] array = {5, 6, 2, 4};
foreach(int test in array)
{
a+=test.toString();
}
output=int.parse(a);
//where output gives you desire out put

.

+2
int result = 0;
int[] arr = { 1, 2, 3, 4};
int multipicator = 1;
for (int i = arr.Length - 1; i >= 0; i--)
{
   result += arr[i] * multipicator;
   multipicator *= 10;
}
+2

( "sstream" )

std; int main() {

int arr[3]={3,2,4};     //your array..

stringstream ss;

ss<<arr[0];   //this can be run as a loop
ss<<arr[1];
ss<<arr[2];


int x;
ss>>x;

cout<<x;        //simply the int arr[3] will be converted to int x..
+1

, , .

, , : .

, (!) 10 ^

5624 : (5 * 10 ^ 3) + (6 * 10 ^ 2) + (2 * 10 ^ 1) + (4 * 10 ^ 0)

: http://en.wikipedia.org/wiki/Horner_scheme

0

:

public int DoConvert(int[] arr)
{

  int result = 0;

  for (int i=0;i<arr.Length;i++)
    result += arr[i] * Math.Pow(10, (arr.Length-1)-i);

  return result; 
}
0

...

arr.Select((item, index) => new { Item = item, Power = arr.Length - (index - 1) }).ToList().ForEach(item => total += (int)(Math.Pow(10, item.Power) * item.Item));
0
var finalScore = int.Parse(array
    .Select(x => x.ToString())
    .Aggregate((prev, next) => prev + next));
0

All Articles