不知何故,我无法在 Google 中找到答案。可能是我在搜索时使用了错误的术语。我正在尝试执行一项简单的任务,将表示字符的数字转换为字符本身,如下表所示:http://unicode-table.com/en/#0460
例如,如果我的号码是 47(即 '\'),我可以将 47 放入 char
并使用 cout
打印它,然后我会看到在控制台中反斜杠(小于256的数字没有问题)。
但是如果我的号码是 1120,字符应该是 'Ѡ'(拉丁语中的 omega)。我假设它由几个字符表示(cout
在打印到屏幕时会知道要转换为 'Ѡ')。
我如何获得代表'Ѡ'的这些“几个字符”?
我有一个名为 ICU 的库,我使用的是 UTF-8。
最佳答案
您所说的Unicode 编号 通常称为代码点。如果您想使用 C++ 和 Unicode 字符串,ICU 提供了一个 icu::UnicodeString 类。你可以find the documentation here .
要创建包含单个字符的 UnicodeString,您可以使用 constructor that takes a code point in a UChar32 :
icu::UnicodeString::UnicodeString(UChar32 ch)
然后你可以调用toUTF8String将字符串转换为 UTF-8 的方法。
示例程序:
#include <iostream>
#include <string>
#include <unicode/unistr.h>
int main() {
icu::UnicodeString uni_str((UChar32)1120);
std::string str;
uni_str.toUTF8String(str);
std::cout << str << std::endl;
return 0;
}
在像 Debian 这样的 Linux 系统上,你可以编译这个程序:
g++ so.cc -o so -licuuc
如果您的终端支持 UTF-8,这将打印一个 omega 字符。
关于c++ - 如何使用 ICU 将 Unicode 代码点转换为 C++ 中的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23322438/