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?
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.
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.
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.
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 ("[]").
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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With