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!
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!
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.
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
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;
}
#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);
}
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;
}
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.
int k = 0;
for (int i = A.length; i > 0; i--){
k += 10 * i * A[A.length - i];
}
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