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)); 10 1704
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 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[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
On Tue, 22 Jun 2004 23:09:36 -0400, John Ilves
<jo*******@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 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[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
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" <jo*******@adelphia.net> wrote in message
news:40**************@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 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[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
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 !!
}
"Magix" <ma***@asia.com> wrote in message news:<40********@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
"Magix" <ma***@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 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
--
Irrwahn Grausewitz (ir*******@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
Magix wrote: 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
In <b0********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes: "Magix" <ma***@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 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <88*************************@posting.google.com> ol************@hotmail.com (Olaf) writes: string[i] = (char)data[i]; // cast int to char
^^^^^^ ^^^^^^^^^^^^^^^^^^^
What for? Leave such conversions to the assignment operator. C is not
Pascal.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de Da*****@cern.ch (Dan Pop) wrote: In <b0********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
"Magix" <ma***@asia.com> wrote:Hi, let say I have an int array, how can I put them together to construct a string?
<snip> You need to provide your own strcpy-like function, e.g:
<snip>/* sample code assumes ASCII character set! */
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
--
Irrwahn Grausewitz (ir*******@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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Klaus Neuner |
last post by:
Hello,
I need a function that converts a list into a set of regexes. Like so:
string_list =
print string_list2regexes(string_list)
This should return something like:
|
by: fudmore |
last post by:
Hello Everybody.
I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.
const int...
|
by: blrmaani |
last post by:
Here is what I want:
string s1 = "This is a list of string";
list<string> s2 = s1.some_method();
Now, I should be able to traverse list s2 and get each
member ( which is of type 'string' ).
...
|
by: na1paj |
last post by:
here's a simple linked list program. the DeleteNode function is
producing an infinit loop i think, but i can't figure out where..
#include <stdio.h>
typedef struct
{
char *str; //str is a...
|
by: Little |
last post by:
Could someone help me figure out how to put my project together. I
can't get my mind wrapped around the creation of the 4 double Linked
Lists. Thank your for your insight.
1. Create 4 double...
| |
by: zacks |
last post by:
I have written a serialized class that has several properties that are
typed as a list of type class. When I deserialize an XML file, the list
is populated just fine. But I am having trouble...
|
by: Little |
last post by:
I have this program and I need to work on the test portion, which tests
if a Val is in the list. It returns false no matter what could you look
at the part and see what might need to be done to fix...
|
by: webmaster |
last post by:
Pardon my being a total C# noob.
I'm trying to take apart the dotNet Time Tracker dotNet C# starterkit
sample application and replicate a part of the code.
At a high level, I have a very...
|
by: Henrik Goldman |
last post by:
Hello,
I have a dataset which consist of a string username and string hostname as a
key and then an integer representing a count as the matching "second" value
in a pair.
So far I've used...
|
by: AZRebelCowgirl73 |
last post by:
This is what I have so far:
My program!
import java.util.*;
import java.lang.*;
import java.io.*;
import ch06.lists.*;
public class UIandDB {
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |