How to convert CString to char* in Visual C++ .NET 2005
-
I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006
-
I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006
yellowine wrote:
char* hello1=new char[hello.GetLength()+1];
Change to
wchar_t
.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
yellowine wrote:
char* hello1=new char[hello.GetLength()+1];
Change to
wchar_t
.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
I use the char* hello1 for an OpenGL function which take strictly a char* parameter. and s wchar_t variable will not work for the gl function.
But does the use of
wchar_t
get rid of the C2664 error? There are ways to convert between Ansi and Unicode.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006
Unicode is turned on by default in 2005, so CString is a Unicode string, and _tcscpy is the wide string copy. 1) Use CStringA instead of CString 2) Use strcpy instead of _tcscpy CStringA hello("CString"); char* hello1=new char[hello.GetLength()+1]; strcpy(hello1, hello); "My dog worries about the economy. Alpo is up to 99 cents a can. That's almost seven dollars in dog money" - Wacky humour found in a business magazine
-
I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006
Dear yellowine, If you need to convert a CString to ASCII in a unicode environment, there are several converting macros and functions available. Outof them, one of the easy to use is ATL conversion macro. Please see the code block below.
#include "ATLBASE.H" . . . CString csString( L"Hello" ); // The UNICODE string. USES_CONVERSION; // Initilizing the conversion macro. char* pszTemp = W2A( csString ); // Convert UNICODE to ASCII. // **NOTE: The W2A allocates string in the stack.** So if you need // ASCII string on heap, you should manually allocate the memory // and strcpy to it.
Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present. -
Dear yellowine, If you need to convert a CString to ASCII in a unicode environment, there are several converting macros and functions available. Outof them, one of the easy to use is ATL conversion macro. Please see the code block below.
#include "ATLBASE.H" . . . CString csString( L"Hello" ); // The UNICODE string. USES_CONVERSION; // Initilizing the conversion macro. char* pszTemp = W2A( csString ); // Convert UNICODE to ASCII. // **NOTE: The W2A allocates string in the stack.** So if you need // ASCII string on heap, you should manually allocate the memory // and strcpy to it.
Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present. -
Easy way to convert CString to char* CString str="JAYARAJ"; char *ch= str.GetBuffer(str.GetLength()); JAYARAJ
J5121982 wrote:
Easy way to convert CString to char*
Which won't work for the case in the question. You'll get the same error that yellowine did.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Easy way to convert CString to char* CString str="JAYARAJ"; char *ch= str.GetBuffer(str.GetLength()); JAYARAJ
As per my understanding, the discuession topic is conversion of CString to char* in unicode envrionment( as UNICODE is default in Visual C++ .NET 2005 from previous posts). Whether this code block will work in a UNICODE defined project? i have tested the same in VS 6.0. But its showing compilation errors. or is this work only with Visual C++ .NET 2005 ? please clarify. I have no experiance with Visual C++ .NET 2005. Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present.
-
I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006
try this, CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; hello1 = hello.GetBuffer(hello.GetLength()); Have A Nice Day! Murali.M