> Programming Languages > C
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on C



C - "put list of int as string" in Programming Languages


Choose Language :
Afrikaans Albanian Arabic Belarusian Bulgarian Catalan Chinese Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician German Greek Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Taiwanese Thai Turkish Ukrainian Vietnamese Welsh Yiddish
Old 06-23-2004   #1
..g..
 
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));


 
Old 06-23-2004   #2
.... ..v..
 
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 ***ign 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
 
Old 06-23-2004   #3
.... ..e..
 
Default Re: put list of int as string

On Tue, 22 Jun 2004 23:09:36 -0400, John Ilves
<johnilves@adelphia.net> 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 ***ign 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: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Old 06-23-2004   #4
..g..
 
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" <johnilves@adelphia.net> wrote in message
news:40D8F470.7070102@adelphia.net...
> 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 ***ign 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



 
Old 06-23-2004   #5
....
 
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 !!
}
 
Old 06-23-2004   #6
.... ....
 
Default Re: put list of int as string

"Magix" <magix@asia.com> wrote in message news:<40d8edd2_1@news.tm.net.my>...
> 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
 
Old 06-23-2004   #7
..rwa.. ..ausewi..
 
Default Re: put list of int as string

"Magix" <magix@asia.com> 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 ***umes 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 (irrwahn33@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
 

Thread Tools
Display Modes






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