在 C++ 中何时使用 WCHAR 何时使用 CHAR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23136837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C++ when to use WCHAR and when to use CHAR
提问by user2179256
I have a question:
我有个问题:
Some libraries use WCHAR as the text parameter and others use CHAR (as UTF-8): I need to know when to use WCHAR or CHAR when I write my own library.
一些库使用 WCHAR 作为文本参数,而其他库使用 CHAR(作为 UTF-8):当我编写自己的库时,我需要知道何时使用 WCHAR 或 CHAR。
回答by Ben Hymers
Use char
and treat it as UTF-8. There are a great many reasons for this; this website summarises it much better than I can:
使用char
并将其视为 UTF-8。这有很多原因;这个网站比我能更好地总结它:
It recommends converting from wchar_t
to char
(UTF-16 to UTF-8) as soon as you receive it from any library, and converting back when you need to pass strings to it. So to answer your question, always use char
except at the point that an API requires you to pass or receive wchar_t
.
它建议您在从任何库接收到它时立即将其转换wchar_t
为char
(UTF-16 到 UTF-8),并在需要将字符串传递给它时转换回来。因此,要回答您的问题,请始终使用char
除非 API 要求您传递或接收wchar_t
.
回答by Mr.C64
WCHAR
(or wchar_t
on Visual C++ compiler) is used for Unicode UTF-16strings.
This is the "native" string encoding used by Win32 APIs.
WCHAR
(或wchar_t
在 Visual C++ 编译器上)用于Unicode UTF-16字符串。
这是 Win32 API 使用的“本机”字符串编码。
CHAR
(or char
) can be used for several other string formats: ANSI, MBCS, UTF-8.
CHAR
(或char
) 可用于其他几种字符串格式:ANSI、MBCS、UTF-8。
Since UTF-16 is the nativeencoding of Win32 APIs, you may want to use WCHAR
(and better a proper string class based on it, like std::wstring
) at the Win32 API boundary, inside your app.
由于 UTF-16 是Win32 API的本机编码,因此您可能希望在应用程序内的 Win32 API 边界处使用WCHAR
(以及更好的基于它的适当字符串类,例如std::wstring
)。
And you can use UTF-8 (so, CHAR
/char
and std::string
) to exchange your Unicode text outside your application boundary. For example: UTF-8 is widely used on the Internet, and when you exchange UTF-8 text between different platforms you don't have the problem of endianness (instead with UTF-16 you have to consider both the UTF-16BE big-endianand the UTF-16LE little-endiancases).
并且您可以使用 UTF-8(so、CHAR
/char
和std::string
)在您的应用程序边界之外交换您的 Unicode 文本。例如:UTF-8 在互联网上被广泛使用,当你在不同平台之间交换 UTF-8 文本时你不存在字节序的问题(而不是 UTF-16 你必须同时考虑 UTF-16BE大- endian和 UTF-16LE little-endian情况)。
You can convert between UTF-16 and UTF-8 using the WideCharToMultiByte()
and MultiByteToWideChar()
Win32 APIs. These are pure-C APIs, and these can be conveniently wrapped in C++ code, using string classes instead of raw character pointers, and exceptions instead of raw error codes. You can find an example of that here.
您可以使用WideCharToMultiByte()
和MultiByteToWideChar()
Win32 API在 UTF-16 和 UTF-8 之间进行转换。这些是纯 C API,可以方便地包装在 C++ 代码中,使用字符串类代替原始字符指针,使用异常代替原始错误代码。你可以在这里找到一个例子。
回答by Pavel Radzivilovsky
The right question is not which type to use, but what should be your contract with your library users. Both char and wchar_t can mean more than one thing.
正确的问题不是使用哪种类型,而是您与图书馆用户的合同应该是什么。char 和 wchar_t 可能意味着不止一件事。
The right answer to me, is use char and consider everything utf-8 encoded, as utf8everywhere.org suggests. This will also make it easier to write cross-platform libraries.
对我来说,正确的答案是使用 char 并考虑所有 utf-8 编码的内容,正如 utf8everywhere.org 所建议的那样。这也将使编写跨平台库变得更加容易。
Make sure you make correct use of strings though. Some APIs like fopen(), would accept a char* string and treat it differently (not as UTF-8) when compiled on Windows. If Unicode is important to you (and it probably is, when you are dealing with strings), be sure to handle your strings correctly. A good example can be seen in boost::locale. I also recommend using boost::nowide on Windows to get strings handled correctly inside your library.
但请确保正确使用字符串。在 Windows 上编译时,某些 API(如 fopen())会接受 char* 字符串并对其进行不同的处理(而不是 UTF-8)。如果 Unicode 对您很重要(并且在您处理字符串时可能很重要),请确保正确处理您的字符串。一个很好的例子可以在 boost::locale 中看到。我还建议在 Windows 上使用 boost::nowide 来正确处理库中的字符串。
回答by Epirocks
In Windows we stick to WCHARS. std::wstring. Mainly because if you don't you end up having to convert because calling Windows functions.
在 Windows 中,我们坚持使用 WCHARS。标准::wstring。主要是因为如果你不这样做,你最终不得不转换,因为调用 Windows 函数。
I have a feeling that trying to use utf8 internally simply because of http://utf8everywhere.org/is gonna bite us in the bum later on down the line.
我有一种感觉,仅仅因为http://utf8everywhere.org/就在内部尝试使用 utf8会在稍后的路线上咬我们。
回答by armanali
It is best recommended that, when developing a Windows application, resort to TCHARs. The good thing about TCHARs is that they can be either regular chars or wchars, depending whether the unicode setting is set or not. Once you resort to TCHARs, you make sure that all string manipulations that you use also start with the _t prefix (e.g. _tcslen for length of string). That way you will know that your code will work both in Unicode and ASCII environments.
最好建议在开发 Windows 应用程序时使用 TCHAR。TCHARs 的好处是它们可以是常规字符或 wchars,具体取决于是否设置了 unicode 设置。一旦你使用了 TCHARs,你要确保你使用的所有字符串操作也以 _t 前缀开头(例如 _tcslen 表示字符串的长度)。这样你就会知道你的代码可以在 Unicode 和 ASCII 环境中工作。