I'm using a library that produces UTF-8 null-terminated strings in the const char*
type. Examples include:
MIGUEL ANTÓNIO
DONA ESTEFÂNIA
I'd like to convert those two const char*
types to CString
so that they read:
MIGUEL ANTÓNIO
DONA ESTEFÂNIA
To that effect, I'm using the following function I made:
CString Utf8StringToCString(const char * s)
{
CStringW ws = CA2W(s, CP_UTF8);
return CW2T(ws);
}
The function seems to do what I want (at least for those 2 cases). However, I'm wondering: is it a good idea at all to use the CA2W macro, followed by CW2T? Am I doing some sort of lossy conversion by doing this? Are there any side-effects I should worry about?
Some other details:
Even if your application is compiled as MBCS, you can still use Unicode strings, buffers, and Windows Unicode APIs without any issue.
Pass your strings around as UTF-8 either with a raw pointer ( const char*
) or in a string class such as CString
or std::string
. When you actually need to render the string for display, convert to Unicode and use the W
API explicitly.
For example:
void UpdateDisplayText(const char* s)
{
CStringW ws = CA2W(s, CP_UTF8);
SetDlgItemTextW(m_hWnd, IDC_LABEL1, ws);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.