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]);
Answer from LPs on Stack Overflowsprintf 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]);
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
c - Transform an array of integers into a string - Stack Overflow
Convert int array to string using C - Stack Overflow
c - Convert integer array into string - Stack Overflow
c - Integer array to string - Code Review Stack Exchange
Is itoa() function standard for integer to string conversion in C?
What’s the use case of integer to string conversion in C programming?
How to convert int to string in C easily?
There is no direct function to do that.
You'll have to use sprintf.
There is nothing in the C stdlib to do this, but it isn't hard to write the string manipulations:
#include <assert.h>
int transform_ints_to_string(int const* data, int data_length,
char* output, int output_length)
{
// if not enough space was available, returns -1
// otherwise returns the number of characters written to
// output, not counting the additional null character
// precondition: non-null pointers
assert(data);
assert(output);
// precondition: valid data length
assert(data_length >= 0);
// precondition: output has room for null
assert(output_length >= 1);
int written = 0;
for (; data_length; data_length--) {
int length = snprintf(output, output_length, "%d", *data++);
if (length >= output_length) {
// not enough space
return -1;
}
written += length;
output += length;
output_length -= length;
}
return written;
}
Example:
int main() {
char buf[100] = "";
int data[] = {1, 2, 3};
if (transform_ints_to_string(data, 3, buf, sizeof buf) == -1) {
puts("not enough room");
}
else {
printf("%s\n", buf);
}
return 0;
}
the following proposed code:
- cleanly compiles
- performs the desired functionality
- demonstrates the use of
strcpy()andsprintf() - avoids the use of 'magic' numbers
- lets the compiler calculate the number of entries in the array[]
and now, the proposed code:
#include <stdio.h>
#include <string.h>
int main( void )
{
int array[] = {1, 6, 2, 4, 4};
char string[255] = {0};
/*do something*/
strcpy( string, "[" );
for( size_t i = 0; i < (sizeof(array)/sizeof(int)) -1; i++ )
{
sprintf( &string[ strlen(string) ], "%d, ", array[i] );
}
sprintf( &string[ strlen(string) ], "%d", array[4] );
strcat( string, "]" );
printf("%s\n", string);
}
a run of the proposed code results in:
[1, 6, 2, 4, 4]
You need to convert the integer into equivalent char using the ascii table subtraction and then strcat to the main string
Copy the array item by item in a loop and store each value in a char. Then null terminate that char array so that it becomes a string. Or alternatively don't declare s3 as int to begin with.
You want something like this:
#include <stdio.h>
#include <string.h>
int main() {
int s3[] = { 97, 115, 100, 102, 103, 49, 50, 51, 37, 36, 33 };
// make sure s3 does not contain values above 127 (or 255 depending on
// your platform).
// temporary storage for the null terminated string
char temp[100]; // must make sure s3 has no more than 99 elements
// copy the values in s3 to temp as chars
int i;
for (i = 0; i < sizeof(s3)/sizeof(int) ; i++)
{
temp[i] = s3[i];
}
// null terminate
temp[i] = 0;
// now we can print it with `printf` and `%s` because
// now `temp` is a null terminated string.
printf("%s\n", temp);
}
sizeof(s3) is the size of the s3 array in bytes, sizeof(int) is the size of an int, therefore sizeof(s3)/sizeof(int) is the number of elements in the s3 array.
Advanced knowledge (slightly above beginner level):
Actually, you should even write
sizeof(s3)/sizeof(*s3)which is cleaner because we don't need to repeat theinttype.Instead of having a fixed size
char temp[100];you could allocate the memory dynamically usingmallocwith the sizesizeof(s3)/sizeof(*s3) + 1(+1 for the null terminator).... or just use
char temp[sizeof(s3)/sizeof(*s3)+1];
you could write a little function
string int_array_to_string(int int_array[], int size_of_array) {
string returnstring = "";
for (int temp = 0; temp < size_of_array; temp++)
returnstring += itoa(int_array[temp]);
return returnstring;
}
untested!
a slightly different approach
string int_array_to_string(int int_array[], int size_of_array) {
ostringstream oss("");
for (int temp = 0; temp < size_of_array; temp++)
oss << int_array[temp];
return oss.str();
}
Do this:
char data_bytes[] = { '0', '0', '1', '1', '0', '0', '3', '3', '\0'};
std::string header(data_bytes, 8);
Or maybe, you want to do this:
std::stringstream s;
s << data_bytes;
std::string header = s.str();
Demo at ideone : http://ideone.com/RzrYY
EDIT:
Last \0 in data_bytes is necessary. Also see this interesting output here: http://ideone.com/aYtlL
PS: I didn't know this before, thanks to Ashot I came to know this difference by experimenting!
This might be better suited for stack overflow but I feel like the question is a bit too short.
I’m trying to convert a multidimensional int array to a string one, I tried using
IntArray.Select(p => p.ToString()).ToArray());
Which I found online but I get an error underlined under the Select part. I am using System.Linq idk if I need anything else or if it doesn’t work for multidimensional arrays or something
Depending on what you really want, there are several possible answers to this question:
int example[5] = {1,2,3,4,5};
char output[5];
int i;
Straight copy giving ASCII control characters 1 - 5
for (i = 0 ; i < 5 ; ++i)
{
output[i] = example[i];
}
characters '1' - '5'
for (i = 0 ; i < 5 ; ++i)
{
output[i] = example[i] + '0';
}
strings representing 1 - 5.
char stringBuffer[20]; // Needs to be more than big enough to hold all the digits of an int
char* outputStrings[5];
for (i = 0 ; i < 5 ; ++i)
{
snprintf(stringBuffer, 20, "%d", example[i]);
// check for overrun omitted
outputStrings[i] = strdup(stringBuffer);
}
#include <stdio.h>
int main(void)
{
int i_array[5] = { 65, 66, 67, 68, 69 };
char* c_array[5];
int i = 0;
for (i; i < 5; i++)
{
//c[i] = itoa(array[i]); /* Windows */
/* Linux */
// allocate a big enough char to store an int (which is 4bytes, depending on your platform)
char c[sizeof(int)];
// copy int to char
snprintf(c, sizeof(int), "%d", i_array[i]); //copy those 4bytes
// allocate enough space on char* array to store this result
c_array[i] = malloc(sizeof(c));
strcpy(c_array[i], c); // copy to the array of results
printf("c[%d] = %s\n", i, c_array[i]); //print it
}
// loop again and release memory: free(c_array[i])
return 0;
}
Outputs:
c[0] = 65
c[1] = 66
c[2] = 67
c[3] = 68
c[4] = 69