|
|||||
|
|
#1 |
|
|
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)); |
|
|
#2 |
|
|
> 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 |
|
|
#3 |
|
|
<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 |
|
|
#4 |
|
|
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 |
|
|
#5 |
|
|
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 !! } |
|
|
#6 |
|
|
"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 |
|
|
#7 |
|
|
"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 |