7

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? Commented Mar 5, 2012 at 10:08
  • 2
    What about array items that are not within the range 0...9? Commented May 24, 2012 at 8:46

14 Answers 14

17

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));
}
Sign up to request clarification or add additional context in comments.

1 Comment

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;}
8
int output = array
    .Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1)))
    .Sum();

Comments

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 Comments

@nawfal thanks, I think this one is the best, upvoted it myself. :)
I love yours. If efficiency doesnt matter, this looks the coolest stackoverflow.com/a/9567257/661933
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 Comments

@FelixK. OP didn't specify he needed time-sensitive code and this is the most readable coded answer.
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.
Use a stringbuilder instead and I will be fine with it . +1 for readability
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;
}

Comments

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 Comment

It returns 4265 - this is not desired result.
1

In C# 3.0 and up:

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

Comments

1

Using LINQ

long result =  array.Aggregate((result, x) => result * 10 + x);

1 Comment

Most elegant solution! To avoid a compilation error use: long result = array.Aggregate((aggregate, x) => aggregate * 10 + x);
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

Comments

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

Comments

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; 
}

Comments

0
var finalScore = int.Parse(array
    .Select(x => x.ToString())
    .Aggregate((prev, next) => prev + next));

Comments

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 Comment

remember to add #include<sstream>
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,

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.