How to convert int array to int?

前端 未结 12 1938
小蘑菇
小蘑菇 2021-01-18 07:49

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:

相关标签:
12条回答
  • 2021-01-18 08:29

    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 讨论(0)
  • 2021-01-18 08:30

    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.

    0 讨论(0)
  • 2021-01-18 08:30
    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;
    }
    
    0 讨论(0)
  • 2021-01-18 08:40

    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];
            }
    
    0 讨论(0)
  • 2021-01-18 08:43

    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 讨论(0)
  • 2021-01-18 08:45
    var finalScore = int.Parse(array
        .Select(x => x.ToString())
        .Aggregate((prev, next) => prev + next));
    
    0 讨论(0)
提交回复
热议问题