Question

I am working on one project where I have stucked on one problem of converting CStringW to CStringA for multibyte string like Japanese Language.

I am loading the string from string resources using LoadString() Method. I have tried following code but it does not seem to work.

CStringW csTest;
csTest.LoadString(JAPANESE_STRING);
CStringA Msg = CStringA(csTest); // Msg has been returned blank string

And

std::string Msg = CW2A(csTest);// Msg has been returned blank string

I have also tried wcstombs() too.

Can anyone tell me how I can convert CStringW to CString? Thanks in advance.

Was it helpful?

Solution

CStringW stores Unicode UTF-16 strings.

What encoding do you expect for your CStringA?

Do you want UTF-8?
In this case, you can do:

// strUtf16 is a CStringW.
// Convert from UTF-16 to UTF-8
CStringA strUtf8 = CW2A(strUtf16, CP_UTF8);

Talking about CStringA without specifying an encoding doesn't make sense.

The second parameter of CW2A is a what is passed to WideCharToMultiByte() Win32 API as CodePage (note that CW2A is essentially a convenient safe C++ RAII wrapper around this API). If you follow this API documentation, you can find several "code page" values (i.e. encodings).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top