How to convert CString to char*.
-
Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------
Uday kiran
Hi , Try this :
CString str = "this is a test";
char* pchar = new char[str.GetLength() + 1];memcpy(pchar , (LPCTSTR)str , str.GetLength() + 1);
Eli
-
try str.GetBuffer(10);
GetBuffer a pointer to the internal CString buffer. it is not a good idea to get this modifiable buffer only if a conversion to char* is needed. there are cast operators for that :
(LPCTSTR)
for instance.
Don't know where to start ?
Refer the Forums Guidelines and ask a friend -
Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------
Uday kiran
CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; //then .... strcpy(pchar, str.GetBuffer()); str.ReleaseBuffer(); It will copy the content from string to char *
-
Hi , Try this :
CString str = "this is a test";
char* pchar = new char[str.GetLength() + 1];memcpy(pchar , (LPCTSTR)str , str.GetLength() + 1);
Eli
eli15021979 wrote:
CString str = "this is a test";
char* pchar = new char[str.GetLength() + 1];
memcpy(pchar , (LPCTSTR)str , str.GetLength() + 1);don't mix ansi, unicode and "T" strings... i suggest this instead :
CString str = _T("this is a test");
TCHAR* pchar = newTCHAR
[str.GetLength() + 1];
memcpy(pchar , (LPCTSTR)str , str.GetLength() + 1);
Don't know where to start ?
Refer the Forums Guidelines and ask a friend -
CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; //then .... strcpy(pchar, str.GetBuffer()); str.ReleaseBuffer(); It will copy the content from string to char *
nooo, don't use GetBuffer() when only a Cast operator does the job. read my previous answer[^]. use GetBuffer() if you need to modify the CString buffer internally
Don't know where to start ?
Refer the Forums Guidelines and ask a friend -
Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------
Uday kiran
strcpy(pchar, (LPCTSTR)str); strcpy(pchar, (const char*)str); strcpy_s(pchar, str.GetLength()+1, (LPCTSTR)str); strcpy_s(pchar, str.GetLength()+1, (const char*)str);
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------
Uday kiran
If you want to use GetBuffer then you need to call ReleaseBuffer
WhiteSky
-
try str.GetBuffer(10);
AHHHH *scream* Stop using GetBuffer!!! :laugh: CString is such a nice encapsulation of char data. GetBuffer is rarely needed. There's plenty of methods and operators to manipulate the string. Mark
-
If you want to use GetBuffer then you need to call ReleaseBuffer
WhiteSky
WhiteSky wrote:
If you want to use GetBuffer then you need to call ReleaseBuffer
Only if you change the contents, right?
-
Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------
Uday kiran
a CString in unicode builds is a wchar_t array, not a char. If you want to store chars in a CString in a unicode build then use the specific CStringA type. If you want to use the generic CString, you'll need to convert the unicode CString to a char type (using the WideCharToMultiByte() or similar). Mark