Integer to Integer Array C#

前端 未结 14 1547
深忆病人
深忆病人 2020-12-08 04:42

I had to split an int \"123456\" each value of it to an Int[] and i have already a Solution but i dont know is there any better way : My solution was :

publi         


        
相关标签:
14条回答
  • 2020-12-08 05:19
    string DecimalToBase(int iDec, int numbase)
            {
                string strBin = "";
                int[] result = new int[32];
                int MaxBit = 32;
                for(; iDec > 0; iDec/=numbase)
                {
                    int rem = iDec % numbase;
                        result[--MaxBit] = rem;
                } 
                for (int i=0;i<result.Length;i++)
                    if ((int)result.GetValue(i) >= base10)
                        strBin += cHexa[(int)result.GetValue(i)%base10];
                    else
                        strBin += result.GetValue(i);
                strBin = strBin.TrimStart(new char[] {'0'});
                return strBin;
            }
            int BaseToDecimal(string sBase, int numbase)
            {
                int dec = 0;
                int b;
                int iProduct=1;
                string sHexa = "";
                if (numbase > base10)
                    for (int i=0;i<cHexa.Length;i++)
                        sHexa += cHexa.GetValue(i).ToString();
                for(int i=sBase.Length-1; i>=0; i--,iProduct *= numbase)
                {
                    string sValue = sBase[i].ToString();
                    if (sValue.IndexOfAny(cHexa) >=0)
                        b=iHexaNumeric[sHexa.IndexOf(sBase[i])];
                    else 
                        b= (int) sBase[i] - asciiDiff;
                    dec += (b * iProduct);
                } 
                return dec; 
            }
    
    0 讨论(0)
  • 2020-12-08 05:21

    Here is a Good Solution for Convert Your Integer into Array i.e: int a= 5478 into int[] There is no issue if You Have a String and You want to convert a String into integer Array for example string str=4561; //Convert into
    array[0]=4;
    array[1]=5;
    array[2]=6;
    array[3]=7;

    Note: The Number of zero (0) in devider are Equal to the Length of input and Set Your Array Length According to Your input length
    Now Check the Coding:

             string str=4587;
                int value = Convert.ToInt32(str);
                int[] arr = new int[4];
                int devider = 10000;
                for (int i = 0; i < str.Length; i++)
                {
                    int m = 0;
                    devider /= 10;
                    arr[i] = value / devider;
                    m = value / devider;
                    value -= (m * devider);
                }
    
    0 讨论(0)
  • 2020-12-08 05:22
    int[] outarry = Array.ConvertAll(num.ToString().ToArray(), x=>(int)x);
    

    but if you want to convert it to 1,2,3,4,5:

    int[] outarry = Array.ConvertAll(num.ToString().ToArray(), x=>(int)x - 48);
    
    0 讨论(0)
  • 2020-12-08 05:27

    You can do that without converting it to a string and back:

    public static int[] intToArray(int num) {
      List<int> numbers = new List<int>();
      do {
        numbers.Insert(0, num % 10);
        num /= 10;
      } while (num > 0);
      return numbers.ToArray();
    }
    

    It only works for positive values, of course, but your original code also have that limitation.

    0 讨论(0)
  • 2020-12-08 05:28

    i had similar requirement .. i took from many good ideas, and added a couple missing pieces .. where many folks weren’t handling zero or negative values. this is what i came up with:

        public static int[] DigitsFromInteger(int n)
        {
            int _n = Math.Abs(n);
            int length = ((int)Math.Log10(_n > 0 ? _n : 1)) + 1;
            int[] digits = new int[length];
            for (int i = 0; i < length; i++)
            {
                digits[(length - i) - 1] = _n % 10 * ((i == (length - 1) && n < 0) ? -1 : 1);
                _n /= 10;
            }
            return digits;
        }
    

    i think this is pretty clean .. although, it is true we're doing a conditional check and several extraneous calculations with each iteration .. while i think they’re nominal in this case, you could optimize a step further this way:

        public static int[] DigitsFromInteger(int n)
        {
            int _n = Math.Abs(n);
            int length = ((int)Math.Log10(_n > 0 ? _n : 1)) + 1;
            int[] digits = new int[length];
            for (int i = 0; i < length; i++)
            {
                //digits[(length - i) - 1] = _n % 10 * ((i == (length - 1) && n < 0) ? -1 : 1);
                digits[(length - i) - 1] = _n % 10;
                _n /= 10;
            }
            if (n < 0)
                digits[0] *= -1;
            return digits;
        }
    
    0 讨论(0)
  • 2020-12-08 05:30

    I believe this will be better than converting back and forth. As opposed to JBSnorro´s answer I reverse after converting to an array and therefore avoid IEnumerable´s which I think will contribute to a little bit faster code. This method work for non negative numbers, so 0 will return new int[1] { 0 }.

    If it should work for negative numbers, you could do a n = Math.Abs(n) but I don't think that makes sense.

    Furthermore, if it should be more performant, I could create the final array to begin with by making a binary-search like combination of if-statements to determine the number of digits.

    public static int[] digitArr(int n)
    {
        if (n == 0) return new int[1] { 0 };
    
        var digits = new List<int>();
    
        for (; n != 0; n /= 10)
            digits.Add(n % 10);
    
        var arr = digits.ToArray();
        Array.Reverse(arr);
        return arr;
    }
    

    Update 2018:

    public static int numDigits(int n) {
        if (n < 0) {
            n = (n == Int32.MinValue) ? Int32.MaxValue : -n;
        }
        if (n < 10) return 1;
        if (n < 100) return 2;
        if (n < 1000) return 3;
        if (n < 10000) return 4;
        if (n < 100000) return 5;
        if (n < 1000000) return 6;
        if (n < 10000000) return 7;
        if (n < 100000000) return 8;
        if (n < 1000000000) return 9;
        return 10;
    }
    
    public static int[] digitArr2(int n)
    {
        var result = new int[numDigits(n)];
        for (int i = result.Length - 1; i >= 0; i--) {
            result[i] = n % 10;
            n /= 10;
        }
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题