8

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

However I want to append the int with the values from the array.

Example:

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

Would be converted into an int that equals 5624.

Thanks for any help in advance.

2
  • 4
    What should happen if the digits in the array comprise a number that cannot fit into an int?
    – Jon
    Mar 5, 2012 at 10:08
  • 2
    What about array items that are not within the range 0...9? May 24, 2012 at 8:46

14 Answers 14

18

simply multiply each number with 10^ his 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));
}
1
  • 2
    In order to be a bit faster Convert.ToInt32(Math.Pow(10, array.Length-i-1)) -> Can be changed to call a custom method : int getIntRank(int rank){ int multiplier=1;for (int k=0; k<rank; k++) multiplier*=10; return multiplier;} Mar 5, 2012 at 10:27
8
int output = array
    .Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1)))
    .Sum();
0
7

Another simple way:

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
}

Trick here is making the array a string of digits then parsing it as integer.

2
3

Use this code you just want to concatenate you int array so use the following code

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

This is not an exact code.

3
  • 3
    @FelixK. OP didn't specify he needed time-sensitive code and this is the most readable coded answer.
    – ediblecode
    Mar 5, 2012 at 10:36
  • Even then, the answer of Shadow Wizard is more readable and safe but the point is it's not a good solution in terms of speed compared with other solutions.
    – Felix K.
    Mar 5, 2012 at 10:43
  • 1
    Use a stringbuilder instead and I will be fine with it . +1 for readability
    – f2lollpll
    Mar 5, 2012 at 21:32
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

Try the following:

        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];
        }
1
  • It returns 4265 - this is not desired result. Mar 5, 2012 at 10:16
1

In C# 3.0 and up:

array.Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1))).Sum();
1

Using LINQ

long result =  array.Aggregate((result, x) => result * 10 + x);
1
  • Most elegant solution! To avoid a compilation error use: long result = array.Aggregate((aggregate, x) => aggregate * 10 + x); Jul 23, 2021 at 17:32
0

This would be easy, if you have understood how the decimal system works.

So let me explain that for you: A decimal digit contains single digits by base ten.

This means you have to iterate through this array (backwards!) and multiply by 10^

For an example 5624 means: (5*10^3) + (6*10^2) + (2*10^1) + (4*10^0)

Please consider also: http://en.wikipedia.org/wiki/Horner_scheme

0

And just for fun...

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

This will do it:

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
var finalScore = int.Parse(array
    .Select(x => x.ToString())
    .Aggregate((prev, next) => prev + next));
0

you can use string stream (include "sstream")

using namespace 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
0

if you want convert int[] to int?[] array , I just use for loop :(

 int[] First = Person.Select(p => p.ShiftID).ToArray();
                int?[] converted = new int?[First.Length];
                for (int im = 0; im < First.Length; im++)
                    converted[im] = First[im];

I cant Find a method to convert better with High Performance,

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.