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
Convert int array to string using C - Stack Overflow
c - Transform an array of integers into a string - Stack Overflow
c - Convert int array to char array - Stack Overflow
c# - How can I convert int array to string? - Stack Overflow
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
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;
}
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
Assuming the "int array" is values in the 0-9 range (which is the only way that makes sense to convert an "int array" length 10 to a 10-character string) - a bit of an exotic way:
string s = new string(Array.ConvertAll(RXBuffer, x => (char)('0' + x)));
But pretty efficient (the char[] is right-sized automatically, and the string conversion is done just with math, instead of ToString()).
Edit: with the revision that makes it clear that these are actually ASCII codes, it becomes simpler:
string s = new string(Array.ConvertAll(RXBuffer, x => (char)x));
Although frankly, if the values are ASCII (or even unicode) it would be better to store it as a char[]; this covers the same range, takes half the space, and is just:
string s = new string(RXBuffer);
All you need is:
string.Join("",RXBuffer);
============== Or =================
int[] RXBuffer = {0,1,2,3,4,5,6,7,8,9};
string result = string.Join(",",RXBuffer);
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
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!
Since charArray is not a string, you can't use the standard functions like strcpy(), or strlen(). Instead, copy every character, and add '\0' at the end. sizeof(charArray) / sizeof(int) can tell you how many characters to copy.
size_t sz = sizeof(charArray) / sizeof(int);
char *string = malloc(sz + 1);
for (int i = 0; i < sz; i++)
{
string[i] = charArray[i];
}
string[sz] = '\0';
You can't convert between int[] and char * in c. You can iterate over the array and build a char* with the desired value, though, then add \0 to the end. You can also typecast, by using (char*) charArray, but this is prone to lots of problems, like the missing \0 terminator. It won't work for strcpy, for example.