How to use Unicode (UTF-8) in C++ [duplicate]

2020-02-23 08:41发布

Possible Duplicate:
Unicode in C++

If I remembered correctly, the default character and string encoding in C++ are ASCII. Is there a simple way to enable Unicode support?

标签: c++ unicode
5条回答
Explosion°爆炸
3楼-- · 2020-02-23 09:01

Current C++ doesn't specify encoding in any way. You might look into an actual Unicode library like ICU or, on some architectures and implementations you can use wchar_t to manipulate and hold Unicode strings.

Edit: This answer was referring to C++03. As noted, it doesn't apply any longer.

查看更多
何必那么认真
4楼-- · 2020-02-23 09:06

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.

查看更多
家丑人穷心不美
5楼-- · 2020-02-23 09:08

If u are using Visual Studio then going into the project properties and defining a Preprocessor as _UNICODE does the job for u.

查看更多
何必那么认真
6楼-- · 2020-02-23 09:09

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).

查看更多
登录 后发表回答