Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i convert an int array into a string array [closed]

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?

like image 611
Zain Aftab Avatar asked May 14 '15 09:05

Zain Aftab


People also ask

Can we convert int array to string?

Convert Array to String Sometimes we need to convert an array of strings or integers into a string, but unfortunately, there is no direct method to perform this conversion. The default implementation of the toString() method on an array returns something like Ljava.

How do I convert a number array to a string?

To convert an array of numbers to an array of strings, call the map() method on the array, and on each iteration, convert the number to a string. The map method will return a new array containing only strings.

Can you turn an int into a string?

We can convert int to String in java using String.valueOf() and Integer.toString() methods. Alternatively, we can use String.format() method, string concatenation operator etc.

Can we convert int array to string in java?

toString(int[]) method returns a string representation of the contents of the specified int array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]").


1 Answers

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]);
like image 53
LPs Avatar answered Nov 15 '22 06:11

LPs