C ++ UTF-8输出与ICU [英] C++ UTF-8 output with ICU

查看:370
本文介绍了C ++ UTF-8输出与ICU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难开始使用C ++ ICU库。我试图得到最简单的例子工作,但即使这失败了。我只想输出一个UTF-8字符串,然后从那里去。

I'm struggling to get started with the C++ ICU library. I have tried to get the simplest example to work, but even that has failed. I would just like to output a UTF-8 string and then go from there.

这里是我有:

#include <unicode/unistr.h>
#include <unicode/ustream.h>

#include <iostream>

int main()
{
    UnicodeString s = UNICODE_STRING_SIMPLE("привет");

    std::cout << s << std::endl;

    return 0;
}

这里是输出:

$ g++ -I/sw/include -licucore -Wall -Werror -o icu_test main.cpp 
$ ./icu_test 
пÑивеÑ

我的终端和字体支持UTF-8,我经常使用UTF-8终端。我的源代码是在UTF-8。

My terminal and font support UTF-8 and I regularly use the terminal with UTF-8. My source code is in UTF-8.

我认为或许我需要将输出流设置为UTF-8,因为ICU存储字符串为UTF-16,我真的不知道,我会认为ustream.h提供的运算符会这样做。

I think that perhaps I somehow need to set the output stream to UTF-8 because ICU stores strings as UTF-16, but I'm really not sure and I would have thought that the operators provided by ustream.h would do that anyway.

任何帮助将不胜感激,谢谢。

Any help would be appreciated, thank you.


推荐答案

$ p> UnicodeString s(привет);

您使用的宏是 仅适用于包含不变字符的字符串,即只有拉丁字母,数字和一些标点符号

如前所述,输入/输出代码页很棘手。你说:

As was said before, input/output codepages are tricky. You said:


我的终端和字体支持UTF-8和
我经常使用终端和
UTF- 8。我的源代码是UTF-8。

My terminal and font support UTF-8 and I regularly use the terminal with UTF-8. My source code is in UTF-8.

这可能是真的,但ICU不知道这是真的。过程代码页可能不同(我们说iso-8859-1),输出代码页可能不同(让我们说shift-jis)。然后,程序将不工作。但是,使用API​​ UNICODE_STRING_SIMPLE的不变字符仍然可以工作。

That may be true, but ICU doesn't know that's true. The process codepage might be different (let's say iso-8859-1), and the output codepage may be different (let's say shift-jis). Then, the program wouldn't work. But, the invariant characters using the API UNICODE_STRING_SIMPLE would still work.

希望这有助于。

srl,icu dev

srl, icu dev

这篇关于C ++ UTF-8输出与ICU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆