In my MFC based application this code:
std::string stdString("MappingName");
std::cout << "as String: " << stdString << std::endl;
CString cString(stdString.c_str());
std::cout << "as CString: " << cString << std::endl;
Produces this output:
as String: MappingName
as CString: 01DEA060
The value of the CString
is different every time I run it, but the length seems to be constant. Some other results are 042219B0
and 042C4378
.
I've tried every variation discussed in this thread, with the results being the same. I've also tried to change the Character Set in the Visual Studio project from Use Unicode Character Set to Use Multi-Byte Character Set, again with no effect.
What could be the reason the conversion fails?
Edit:
More tests show that the value of the std::string
doesn't seem to make a difference:
tmp as String: The quick brown fox jumps over the lazy dog
tmp as CString: 00EAAF88
I've also set the Character Set to Not Set which didn't help either.
The problem is printing not conversion.
CString
can implicilty convert to TCHAR const*
. When built with unicode enabled, this is wchar_t const*
.
std::cout
has no wchar_t const*
overload for <<
. It does have a void const*
overload.
The void pointer overload prints a pointer address in hex.
Cast to CStringA
before printing:
std::cout << "as CString: " << static_cast<CStringA>(cString) << std::endl;
Or print using wcout
.