Click to See Complete Forum and Search --> : Converting CStrings to char*


the groove king
May 8th, 1999, 05:24 PM
How do I convert from a CString to a character array.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
ie:
char* string1;
CString string2("Hello");
/*
How would I go about putting Hello
into string1?
*/
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Thanks,
Phil

Dan Haddix
May 8th, 1999, 07:12 PM
Try this...

char* string1;
CString string2("Hello");
string1 = string2.GetBuffer(-1);

nms
May 10th, 1999, 12:44 PM
char* string1;
CString string2("Hello");
// Try this...
string1 = new char[string2.GetLength()];
strcpy(string1,(LPCTSTR)string2);

this should work....


nms

chandra_s
May 10th, 1999, 04:17 PM
char* s1;
CString s2 ("Hello");

s1 = (char*)(const char*)s2;
// This will xfer the contents of s2 to s1

Cheers!
Chandra

sally
May 10th, 1999, 08:16 PM
I'd prefer this one:

char* string1 = NULL;
CString string2("Hello");
// Try this...
string1 = new char[string2.GetLength() + 1];
::strcpy(string1, string2);

Sally

Sally
May 10th, 1999, 08:16 PM
I'd prefer this one:

char* string1 = NULL;
CString string2("Hello");
// Try this...
string1 = new char[string2.GetLength() + 1];
::strcpy(string1, string2);

Sally

sally
May 10th, 1999, 08:17 PM
Nope it won't. it will make s1 POINT to s2 which is different

Sally

Sally
May 10th, 1999, 08:17 PM
Nope it won't. it will make s1 POINT to s2 which is different

Sally