C语言 我如何将 int 数组存储到字符串中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30234363/
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 11:56:39  来源:igfitidea点击:

how can i store an int array into string

carraysstringcharint

提问by Zain Aftab

I have an integer array:

我有一个整数数组:

int a[5]={5,21,456,1,3}

I need to store these number into chararray so that the char array will have some thing like this:

我需要将这些数字存储到char数组中,这样字符数组就会有这样的东西:

char *s="52145613";

Is there any library function in cfor this?

c中是否有任何库函数?

回答by LPs

sprintfdo what you need.

sprintf做你需要的。

Little example

小例子

char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
   index += sprintf(&str[index], "%d", a[i]);

snprintftakes care of the str length

snprintf处理 str 长度

char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
   index += snprintf(&str[index], 128-index, "%d", a[i]);

回答by David C. Rankin

What you need to do is to convert the integer values in ainto character values in sleaving room for a null-terminating character. In your case a total of 9 characters. sprintfis the proper tool since vales exceed single digits:

您需要做的是将整数值转换a为字符值,s为空终止字符留出空间。在您的情况下,共有 9 个字符。sprintf是合适的工具,因为值超过个位数:

#include <stdio.h>

int main(void)
{
    int a[5]={5,21,456,1,3};
    char s[9] = {0};
    int n = 0;

    for (int i = 0; i < 5; i++) {
        n += sprintf (&s[n], "%d", a[i]);
    }

    printf ("\n char* s = %s\n\n", s);

    return 0;
}

Output

输出

$ ./bin/sprintf

 char* s = 52145613

回答by Sebastian-Lauren?iu Plesciuc

Tried to go for a more readable approach even though the other answers are more efficient in terms of memory and instructions.

尽管其他答案在内存和指令方面更有效,但仍试图采用更具可读性的方法。

Tested it here.

在这里测试了它。

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

void get_me_a_string(int * int_array, int array_size, char * output_string, int output_string_max_size)
{
    if(!int_array || !output_string)
        return;

    char * aux_string = NULL;

    //Depending on the compiler int is 2-byte or 4 byte.
    //Meaning INT_MAX will be at most 2147483647 (10 characters + 1 '
#include <stdio.h>

int main(void) 
{
    int a[] = { 5, 21, 456, 1, 3 };
    const size_t N = sizeof( a ) / sizeof( *a );
    char s[10];
    int n;

    for ( size_t i = 0, j = 0; 
          i < N && ( n = snprintf( s + j, sizeof( s ) - j, "%d", a[i] ) ) > 0;
          i++ )
    {
        j += n;
    }        

    puts( s );

    return 0;
}
'). aux_string = (char *) malloc(11); if(!aux_string) return; int i; int current_array_size = 0; for(i = 0; i < array_size; i++) { sprintf(aux_string, "%d", int_array[i]); current_array_size += strlen(aux_string); if(current_array_size < output_string_max_size) strcat(output_string, aux_string); else break; } free(aux_string); } int main(void) { int a[5]={5,21,456,1,3}; int string_max_size = 256; char * string_from_array = NULL; string_from_array = (char *) malloc(string_max_size); if(NULL == string_from_array) { printf("Memory allocation failed. Exiting..."); return 1; } memset(string_from_array, 0, string_max_size); get_me_a_string(a, 5, string_from_array, string_max_size); printf(string_from_array); free(string_from_array); return 0; }

回答by Sourav Ghosh

You can search for sprintf()/ snprintf()which can get the job done for you.

您可以搜索sprintf()/snprintf()可以为您完成工作。

From the man page,

从手册页,

int sprintf(char *str, const char *format, ...);int snprintf(char *str, size_t size, const char *format, ...);

The functions in the printf()family produce output according to a format as described below....[...]...

sprintf(), snprintf(), vsprintf()and vsnprintf()write to the character string str.

int sprintf(char *str, const char *format, ...);int snprintf(char *str, size_t size, const char *format, ...);

printf()系列中的函数根据如下所述的格式生成输出。……[……]……

sprintf(), snprintf(),vsprintf()vsnprintf()写入字符串str

回答by Vlad from Moscow

Here is a demonstrative program that shows how this can be done.

这是一个演示程序,展示了如何做到这一点。

52145613

The program output is

程序输出是

##代码##