如何在 C++ 中使用 Unicode (UTF-8)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2993103/
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
How to use Unicode (UTF-8) in C++
提问by segfault
Possible Duplicate:
Unicode in C++
可能的重复:
C++ 中的 Unicode
If I remembered correctly, the default character and string encoding in C++ are ASCII. Is there a simple way to enable Unicode support?
如果我没记错的话,C++ 中的默认字符和字符串编码是 ASCII。有没有一种简单的方法来启用 Unicode 支持?
采纳答案by Edward Strange
Current C++ doesn't specify encoding in any way. You might look into an actual Unicode library like ICU or, on somearchitectures and implementations you can use wchar_t to manipulate and hold Unicode strings.
当前的 C++ 没有以任何方式指定编码。您可能会查看像 ICU 这样的实际 Unicode 库,或者,在某些体系结构和实现中,您可以使用 wchar_t 来操作和保存 Unicode 字符串。
Edit: This answer was referring to C++03. As noted, it doesn't apply any longer.
编辑:这个答案是指 C++03。如前所述,它不再适用。
回答by Bjarke Freund-Hansen
回答by Pete Kirkham
It rather depends what you want to do with the text you are processing. Half the point of UTF-8 is that you don't need to change existing code if it handles 8-bit chars and does nothing special with characters above 128. Of course, strlen is the length in bytes rather than the character or code-point count. So it may be that you have a text in, text out program that can use UTF-8 directly. Or it may be that you're creating a GUI in text and so need to handle ruby and RTL text, in which case your job is much more complicated and you probably need to chose appropriate libraries.
这更取决于您想对正在处理的文本做什么。UTF-8 的一半之处在于,如果它处理 8 位字符并且对 128 以上的字符没有任何特殊作用,则您不需要更改现有代码。当然,strlen 是以字节为单位的长度,而不是字符或代码-点数。因此,您可能有一个可以直接使用 UTF-8 的文本输入、文本输出程序。或者,您可能正在以文本形式创建 GUI,因此需要处理 ruby 和 RTL 文本,在这种情况下,您的工作要复杂得多,您可能需要选择合适的库。
回答by Billy ONeal
Depends on the version of C++ you are using. C++0x (not entirely released yet but still supported on many compilers) adds native UTF-8 support to the language. Otherwise, no the language does not support UTF-8. C++03 and earlier support unicode through the use of Wide Characters (wchar_t).
取决于您使用的 C++ 版本。C++0x(尚未完全发布,但仍被许多编译器支持)为该语言添加了原生 UTF-8 支持。否则,没有该语言不支持 UTF-8。C++03 及更早版本通过使用宽字符 (wchar_t) 支持 unicode。
回答by Anuj
If u are using Visual Studio then going into the project properties and defining a Preprocessor as _UNICODE does the job for u.
如果您使用的是 Visual Studio,则进入项目属性并将预处理器定义为 _UNICODE 为您完成这项工作。