10

How can I convert an array of 6 integers into a single integer. Example provided below of what I want to do.

Array: {0, 1, 2, 3, 4, 5, 6}

Integer: 123456

Thank you!

7
  • Do you realize that this will not be possible if the 6 integers in the array are too big? Commented Oct 25, 2013 at 21:02
  • 1
    What have you tried? Also, if you get that far, you'll find that the leading zero will disappear: in your example, for instance, the integer will be 123456. This happens for good reason and should not alarm you. Commented Oct 25, 2013 at 21:03
  • Yes, assuming they are not too big. Commented Oct 25, 2013 at 21:03
  • 7
    So you mean an array of digits? Let me tell you, how the decimal system works: Value=digit(n)+10*digit(n-1)+100*digit(n-2)+...+10^n*digit(0) Commented Oct 25, 2013 at 21:04
  • 2
    Beware arithmetic overflow. Commented Oct 25, 2013 at 21:15

8 Answers 8

18

Try this:

int i, k = 0;
for (i = 0; i < n; i++)
    k = 10 * k + a[i];

where n is the length of the array. This is true, however, when the array is short enough otherwise you would get an int overflow.

Sign up to request clarification or add additional context in comments.

1 Comment

int a[]={1,3,5,234,1,5,3}; Bounds were not mentioned, but could be a problem :)
1

Here is a function I made

int array_to_num(int arr[],int n){
    char str[6][3];
    int i;
    char number[13] = {'\n'};

    for(i=0;i<n;i++) sprintf(str[i],"%d",arr[i]);
    for(i=0;i<n;i++)strcat(number,str[i]);

    i = atoi(number);
    return i;
} 

where str[6][3] means there are 6 elements that can hold 2 digit numbers, change it to suit your needs better. Also n is the size of the array you put into the function. Use it like this:

int num[6] = {13,20,6,4,3,55};
int real_num;
real_num = array_to_num(num,6);

real_num will now be 132064355

1 Comment

What about arithmetic overflow?
1

try this one:

#include <stdio.h>
#include <math.h>

int main()
{
    int arr[] = {1, 2, 2, 43, 4, 27};
    int size = sizeof(arr)/sizeof(arr[0]);
    int n = 0;
    int number  = 0;
    int val  = 0;
    while(n <size)
    {
        val = arr[n];
        while(val!= 0)
        {
            val = val/10;
            number = number*10;
        }
        number = number + arr[n];
        n++;
    }
    printf("%d", number);
    return 0;
}

Comments

0
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv){
    int n;
    char buff[100];

    sprintf(buff,"%d%d%d%d%d%d%d", 0, 1,2, 3, 4, 5, 6);
    n = atoi(buff);

    printf("the number is %d",n);

}

another version

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv){
    int n;
    int i;
    char buff[100];
    int x[]={0,1,2,3,4,5,6};
    for (i=0; i<7; i++) {
        sprintf(&buff[i],"%d",x[i]);
    }
    n = atoi(buff);

    printf("the number is %d",n);

}

Comments

0

Be aware that the range of integer values are: –2,147,483,648 to 2,147,483,647.
Any list of numbers in an array (such as what you are describing) will need something bigger than an int to hold value for which the number of digits representing that value is greater than 10, and then, the first digit can only be 2 or less, and so on... (if you want more digits, use __int64)

This will return an integer comprised of the elements of an int array...

(note, things like negative values are not accounted for here)

#include <ansi_c.h>
int ConcatInts(int *a, int numInts);
int main(void)
{
    int a;
    int m[]={1,2,3,4,5,6,7,8,9};
    int size = sizeof(m)/sizeof(m[0]);

    a = ConcatInts(m, size); //a = 123456789

    return 0;   
}

int ConcatInts(int *a, int numInts)
{
    int i=0, size;
    int b=0;
    int mult = 1;
    size = sizeof(a)/sizeof(a[0]);
    for(i=0;i<numInts;i++)
    {
        if((a[i] < 0) ||(a[i]>9)) return -1;
        if(i==0)
        {
            b += a[i];
        }
        else
        {
            b *= 10;
            b += a[i];
        }
    }
    return b;
}

1 Comment

The special case test if (i==0) is not necessary, since b starts out as 0 and so 10*b is also 0. More important: you cannot use sizeof on an array pointer. Fortunately, it just adds dead code.
0

Maybe convert the array values into a string and then cast to an Int when necessary? Of course, being aware of limits, as already mentioned.

Comments

0
int k = 0;
for (int i = A.length; i > 0; i--){
    k += 10 * i * A[A.length - i];
}

Comments

0

Why not just convert each item in the int[] to a string, and add the strings together, and then convert that back into an integer

1 Comment

Thanks for the idea. But it would be better to put this as a comment for the question

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.