如何将Unicode代码点转换为C ++中的字符使用ICU? [英] How to convert a Unicode code point to characters in C++ using ICU?
问题描述
不知怎的,我在Google找不到答案。我在搜索时可能使用了错误的术语。我试图执行一个简单的任务,将表示一个字符的数字转换为字符本身,如下表: http://unicode-table.com/en/#0460
Somehow I couldn't find the answer in Google. Probably I'm using the wrong terminology when I'm searching. I'm trying to perform a simple task, convert a number that represents a character to the characters itself like in this table: http://unicode-table.com/en/#0460
例如,如果我的号码是47(即\ ),我可以把47放在 char
中并使用 cout
打印,我会在控制台看到一个反斜杠(对于低于256的数字没有问题)。
For example, if my number is 47 (which is '\'), I can just put 47 in a char
and print it using cout
and I will see in the console a backslash (there is no problem for numbers lower than 256).
但是如果我的数字是1120,字符应该是'Ѡ'我假设它由几个字符表示( cout
在打印到屏幕时会知道转换为'Ѡ')。
But if my number is 1120, the character should be 'Ѡ' (omega in Latin). I assume it is represented by several characters (which cout
would know to convert to 'Ѡ' when it prints to the screen).
如何获取这些代表Ѡ的几个字符?
How do I get these "several characters" that represent 'Ѡ'?
我有一个名为ICU的库,我使用UTF-8
I have a library called ICU, and I'm using UTF-8.
推荐答案
所谓的 Unicode号码通常称为 >。如果要使用C ++和Unicode字符串,ICU会提供一个 icu :: UnicodeString 类。您可以在此处查找文档。
What you call Unicode number is typically called a code point. If you want to work with C++ and Unicode strings, ICU offers a icu::UnicodeString class. You can find the documentation here.
要创建一个包含单个字符的 UnicodeString ,您可以使用构造函数在 UChar32 中采用代码点:
To create a UnicodeString holding a single character, you can use the constructor that takes a code point in a UChar32:
icu::UnicodeString::UnicodeString(UChar32 ch)
Then you can call the toUTF8String method to convert the string to 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系统上, p>
On a Linux system like Debian, you can compile this program with:
g++ so.cc -o so -licuuc
如果您的终端支持UTF-8,这将打印一个欧米茄字符。
If your terminal supports UTF-8, this will print an omega character.
这篇关于如何将Unicode代码点转换为C ++中的字符使用ICU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!