简体   繁体   English

Visual C++ - UTF-8 - CA2W 后跟带有 MBCS 的 CW2T - 可能是个坏主意?

[英]Visual C++ - UTF-8 - CA2W followed by CW2T with MBCS - Possibly a bad idea?

I'm using a library that produces UTF-8 null-terminated strings in the const char* type.我正在使用一个以const char*类型生成 UTF-8 空终止字符串的库。 Examples include:例子包括:

MIGUEL ANTÓNIO
DONA ESTEFÂNIA

I'd like to convert those two const char* types to CString so that they read:我想将这两个const char*类型转换为CString以便它们读取:

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?但是,我想知道:使用 CA2W 宏然后使用 CW2T 是个好主意吗? Am I doing some sort of lossy conversion by doing this?我这样做是在进行某种有损转换吗? Are there any side-effects I should worry about?有什么副作用我应该担心吗?

Some other details:其他一些细节:

  1. I'm using Visual Studio 2015我正在使用 Visual Studio 2015
  2. My application is compiled using Use Multi-Byte Character Set我的应用程序是使用使用多字节字符集编译的

Even if your application is compiled as MBCS, you can still use Unicode strings, buffers, and Windows Unicode APIs without any issue.即使您的应用程序被编译为 MBCS,您仍然可以毫无问题地使用 Unicode 字符串、缓冲区和 Windows Unicode API。

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 .使用原始指针( const char* )或字符串类(如CStringstd::string )将字符串作为 UTF-8 传递。 When you actually need to render the string for display, convert to Unicode and use the W API explicitly.当您实际需要呈现字符串以供显示时,请转换为 Unicode 并显式使用W API。

For example:例如:

void UpdateDisplayText(const char* s)
{
    CStringW ws = CA2W(s, CP_UTF8);
    SetDlgItemTextW(m_hWnd, IDC_LABEL1, ws);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM