put list of int as string

M

Magix

Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116

the string will be "My Test"


I'm not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));
 
J

John Ilves

Magix said:
Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116

the string will be "My Test"


I'm not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));
Well a string is just an array of charS, so you want to assign element n
of the int array to element n of the char array.

char* intarr2str(int* arr, int len)
{
char* str;
int i;

str = malloc(len + 1); /* room for null */

for(i = 0; i < len; ++i)
str = arr;

str = NULL; /* null terminate */

return str; /* caller must free */
}

In your case you would use that with something like this:

char* mystring;
mystring = intarr2str(data, 7);

Out of curiosity, why do you want to do this?

John
 
J

Jack Klein

Magix said:
Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116

the string will be "My Test"


I'm not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));
Well a string is just an array of charS, so you want to assign element n
of the int array to element n of the char array.

char* intarr2str(int* arr, int len)
{
char* str;
int i;

str = malloc(len + 1); /* room for null */

for(i = 0; i < len; ++i)
str = arr;

str = NULL; /* null terminate */

return str; /* caller must free */
}


[snip]

Adding:

#include <stdlib.h>

....for malloc's prototype and a definition of the null pointer
constant NULL, here is the result from two different compilers:

========
Compiling...
sample.c
C:\Program Files\Microsoft Visual
Studio\MyProjects\sample\sample.c(13) : warning C4047: '=' : 'char '
differs in levels of indirection from 'void *'

sample.obj - 0 error(s), 1 warning(s)
========

....and:

========
Wedit output window build: Tue Jun 22 23:46:32 2004
Error c:\prog\lcc\projects\sample\sample.c: 13 operands of = have
illegal types 'char' and 'pointer to void'
Compilation + link time:0.1 sec, Return code: 1
========

Whoever told you that NULL is equivalent to '\0'? Certainly not the C
language standard, and not most of the compilers I have ever used.
 
M

Magix

Thanks. it for some serial communication stuff.

What if it is 16 bits data (2 bytes), how can I use buffer hold the data ?
In the end, I still want the outcome as a string.

John Ilves said:
Magix said:
Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116

the string will be "My Test"


I'm not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));
Well a string is just an array of charS, so you want to assign element n
of the int array to element n of the char array.

char* intarr2str(int* arr, int len)
{
char* str;
int i;

str = malloc(len + 1); /* room for null */

for(i = 0; i < len; ++i)
str = arr;

str = NULL; /* null terminate */

return str; /* caller must free */
}

In your case you would use that with something like this:

char* mystring;
mystring = intarr2str(data, 7);

Out of curiosity, why do you want to do this?

John
 
O

Olaf

Hello

What are you trying to do
A pointer has no varable space so you cant give it a value !!!

your program should look like this
In C you must always define you memory. it does not apear from nothing!!


{
int data[7], i;
char string[8];
// give the array his values
data[0] = 77;
data[1] = 121;
data[2] = 32;
data[3] = 84;
data[4] = 101;
data[5] = 115;
data[6] = 116;

for(i=0; i < 7; i++)
string = (char)data; // cast int to char
string[7] = '\0'; // make the next char a null char this is end of string !!
}
 
R

Ravi Uday

Magix said:
Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116

the string will be "My Test"


I'm not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));

Have you tried compiling your program and fixing the warnings/errors
???? If you had done then you know the answer !!

Heres a solution:

bash-2.02$ cat temp.c
#include <stdio.h>
#include <string.h>

int main ()
{

int data[10] ,i;
char mystring[10];

data[0] = 77;
data[1] = 121;
data[2] = 32;
data[3] = 84;
data[4] = 101;
data[5] = 115;
data[6] = 116;

for (i=0; i<7; i++)
{
mystring = (char)data;
printf("%c",mystring);
}
printf("\n");
return 0;
}

bash-2.02$ gcc -ansi -pedantic -Wall temp.c
bash-2.02$ ./a.exe
My Test
bash-2.02$

- Ravi
 
I

Irrwahn Grausewitz

Magix said:
Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116

the string will be "My Test"


I'm not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));

Even if you correctly allocate memory for mystring to point to: no,
it doesn't. You need to provide your own strcpy-like function, e.g:

/* intarrtostr requires the int array to be zero-terminated! */

char *intarrtostr( char *dst, const int *src )
{
char *p = dst;

while ( ( *p++ = *src++ ) != '\0' )
continue;
return dst;
}

/* sample code assumes ASCII character set! */

#include <stdio.h>

int main( void )
{
int data[] = { 77, 121, 32, 84, 101, 115, 116, 0 };
char mystring[ sizeof data ];

puts( intarrtostr( mystring, data ) );
return 0;
}

HTH
Regards
 
D

Dan Pop

In said:
string = (char)data; // cast int to char

^^^^^^ ^^^^^^^^^^^^^^^^^^^
What for? Leave such conversions to the assignment operator. C is not
Pascal.

Dan
 
D

Dan Pop

In said:
Magix said:
Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116

the string will be "My Test"


I'm not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));

Even if you correctly allocate memory for mystring to point to: no,
it doesn't. You need to provide your own strcpy-like function, e.g:

/* intarrtostr requires the int array to be zero-terminated! */

char *intarrtostr( char *dst, const int *src )
{
char *p = dst;

while ( ( *p++ = *src++ ) != '\0' )
continue;
return dst;
}

/* sample code assumes ASCII character set! */

Nope, no such assumption in the sample code. Only in the OP's
expectations.
#include <stdio.h>

int main( void )
{
int data[] = { 77, 121, 32, 84, 101, 115, 116, 0 };

The OP's input array is not zero-terminated...
char mystring[ sizeof data ];

char mystring[sizeof data / sizeof *data];

There is no point in wasting memory...
puts( intarrtostr( mystring, data ) );
return 0;
}

Dan
 
J

John Ilves

Magix said:
Thanks. it for some serial communication stuff.

What if it is 16 bits data (2 bytes), how can I use buffer hold the data ?
In the end, I still want the outcome as a string.

You mean an array of short intS? That's fine it, would work the same way
for any type of int.

John
 
I

Irrwahn Grausewitz

Nope, no such assumption in the sample code. Only in the OP's
expectations.

Nitpickery. ;o)
#include <stdio.h>

int main( void )
{
int data[] = { 77, 121, 32, 84, 101, 115, 116, 0 };

The OP's input array is not zero-terminated...

Well, writing a strncpy-like solution was silently left as an
exercise to the OP. :)
char mystring[ sizeof data ];

char mystring[sizeof data / sizeof *data];

There is no point in wasting memory...

Touché. Thanks for correction.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top