how can i convert an int array into a string array
Solution 1
sprintf
do what you need.
Little example
char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
index += sprintf(&str[index], "%d", a[i]);
snprintf
takes care of the str length
char str[128];
int i=0;
int index = 0;
for (i=0; i<5; i++)
index += snprintf(&str[index], 128-index, "%d", a[i]);
Solution 2
What you need to do is to convert the integer values in a
into character values in s
leaving room for a null-terminating character. In your case a total of 9 characters. sprintf
is the proper tool since vales exceed single digits:
#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
Solution 3
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 '\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;
}
Solution 4
You can search for sprintf()
/ snprintf()
which can get the job done for you.
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()
andvsnprintf()
write to the character stringstr
.
Zain Aftab
Updated on July 05, 2022Comments
-
Zain Aftab almost 2 years
I have an integer array:
int a[5]={5,21,456,1,3}
I need to store these number into
char
array so that the char array will have some thing like this:char *s="52145613";
Is there any library function in c for this?
-
David C. Rankin almost 9 yearsGot it -- no coffee yet - nuking ans.
-
LPs about 7 years@WoodrowBarlow Yes, I edited
-
scai about 7 yearsBeware that
snprintf()
returns a negative value in case of errors. Adding this value toindex
will be wrong. -
Fremzy about 3 yearsThis doesn't work if number of digits in resulting string is > 128
-
Fremzy about 3 yearsThis can be further extended by making string_max_size dynamic ,
string_max_size = sizeof(a) / sizeof(a[0]*11;