> Programming Languages > C
Various Topics Home

C



C Programming Languages put list of int as string


Default put list of int as string

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));


Default Re: put list of int as string

Magix wrote:
> 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 hostinghostinghostingign 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[i] = arr[i];

str[i] = 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
Default Re: put list of int as string

On Tue, 22 Jun 2004 23:09:36 -0400, John Ilves
<> wrote in comp.lang.c:

> Magix wrote:
> > 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 hostinghostinghostingign 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[i] = arr[i];
>
> str[i] = 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.

--
Jack Klein
Home:
FAQs for
comp.lang.c
comp.lang.c++
alt.comp.lang.learn.c-c++

Default Re: put list of int as string

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" <> wrote in message
news:
> Magix wrote:
> > 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 hostinghostinghostingign 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[i] = arr[i];
>
> str[i] = 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



Default Re: put list of int as string

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[i] = (char)data[i]; // cast int to char
string[7] = '\0'; // make the next char a null char this is end of string !!
}
Default Re: put list of int as string

"Magix" <> wrote in message news:<>...
> 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[i] = (char)data[i];
printf("%c",mystring[i]);
}
printf("\n");
return 0;
}

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

- Ravi
Default Re: put list of int as string

"Magix" <> wrote:
>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 hostinghostinghostingumes 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
--
Irrwahn Grausewitz ()
welcome to clc:
clc faq-list :
clc OT guide :

Thread Tools
Display Modes



Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0