Re: Problem with converting std::string to CString
Originally Posted by LeanA
Hi all, thank you all for replying.
I forgot to mention that I also tried wcout. It has the same output as in using cout.
Or does wcout not work with CStrings too?
There are two "versions" of CString, and it depends on the build that you are producing. If it's UNICODE build, then CString will contain a wide character string, else it will contain a string that is single-byte.
That is the first issue -- sort out what build you are producing, Unicode or not Unicode. If it's Unicode, then the stream must be std::wcout when printing wide character strings, else the stream to use is std::cout.
The second issue is that operator << knows nothing about CString -- that's why you get the hex output even if you are using std::wcout and you have a Unicode string.
To tell operator << how to handle CString, the CString class has a casting operator (LPCTSTR) that returns a const char* (or const w_chart*) to operator <<, which that operator is overloaded to handle.
So you have to make two things happen -- you use the correct stream (cout or wcout), depending on the build type (Unicode, non-Unicode) and you use the LPCTSTR cast operator for CString.