Abhishek Srivastava Abhishek Srivastava - 5 months ago 34
C++ Question

Converting CStringW to CString resulting question mark

My vc++ project's property "character set" is set to "Use Multi-Byte Character", I am converting CStringW (Chinese language string) into CString and it's converting the Chinese characters into question marks.

CStringA utf8;
int cc=0;
// get length (cc) of the new multibyte string excluding the \0 terminator first
if ((cc = WideCharToMultiByte(CP_UTF8, 0, stringw, -1, NULL, 0, 0, 0) - 1) > 0)
// convert
char *buf = utf8.GetBuffer(cc);
if (buf) WideCharToMultiByte(CP_UTF8, 0, stringw, -1, buf, cc, 0, 0);

Please help, I'm new in c++.


The conversion is working correctly, you can send the UTF-8 result to another program which requires UTF-8, or test it in a web browser, or test it in Notepad with UTF-8 encoding.

The only problem you may face is that Windows API cannot directly display UTF-8 string, so it may look as if it failed. For example, ::MessageBoxA(0, utf8, 0, 0) will show the wrong result.

To see if the conversion worked, you can convert back to UTF-16. For example:

#include <atlstr.h>

CStringW utf16 = L"汉字 / 漢字";
CStringA utf8 = CW2A(utf16, CP_UTF8);
CStringW copy = CA2W(utf8, CP_UTF8);

if (copy == utf16)
    ::MessageBoxW(0, copy, L"success", 0);
    ::MessageBoxW(0, copy, L"failed", 0);