C语言 如何将整数数组转换为C中的整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19599364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:49:46  来源:igfitidea点击:

How to convert array of integers into an integer in C?

carrays

提问by Rob Johnson

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

如何将 6 个整数的数组转换为单个整数。下面提供了我想要做的示例。

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

大批: {0, 1, 2, 3, 4, 5, 6}

Integer: 123456

整数: 123456

Thank you!

谢谢!

回答by sve

Try this:

尝试这个:

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

where nis the length of the array. This is true, however, when the array is short enough otherwise you would get an intoverflow.

其中n是数组的长度。然而,这是真的,当数组足够短时,否则你会int溢出。

回答by Zeldamaster7778

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 6elements that can hold 2digit numbers, change it to suit your needs better. Also nis the size of the array you put into the function. Use it like this:

wherestr[6][3]意味着有些6元素可以保存2数字,请更改它以更好地满足您的需求。也是n您放入函数的数组的大小。像这样使用它:

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

real_numwill now be 132064355

real_num现在将是 132064355

回答by clody

    #include <stdio.h>

    char* arr2str(int arr[], int size) {
        static char buffer[256];
        memset(&buffer[0], 0, sizeof(buffer)/sizeof(char));
        char *ptr = &buffer[0];
        for(int i=0; i<size; ++i) {
            sprintf(ptr += strlen(ptr), "%d", arr[i]);
        }
        return buffer;
    }

    int arr2int(int arr[], int size) {
        char buffer[256] = {0,};
        char *ptr = &buffer[0];
        for(int i = 0; i < size; ++i) {
            sprintf(ptr += strlen(ptr), "%d", arr[i]);
        }
        return atoi(&buffer[0]);
    }

    int main(int argc, const char * argv[]) {

        int arr[] = {1,2,3,4,5,6,7,8,9,0};
        int size = sizeof(arr) / sizeof(int);
        char *str = arr2str(arr, size);
        int num = arr2int(arr, size);
        printf("%s, %d", str, num);
    }

回答by Ben Beizer

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

回答by shivam Singhal

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

回答by Vorsprung

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

}

回答by ryyker

Be aware that the range of integer valuesare: –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)

请注意整数值范围是:–2,147,483,648 到 2,147,483,647。
数组中的任何数字列表(例如您所描述的)都需要大于 int 的值来保存表示该值的位数大于 10 的值,然后,第一个数字只能是 2 或更少,依此类推...(如果您想要更多数字,请使用__int64

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

这将返回一个由 int 数组元素组成的整数...

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

回答by MacNick

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?当然,正如已经提到的,要注意限制。