将ICU UnicodeString转换为平台相关的char *(或std :: string)

时间:2010-12-08 11:13:08

标签: c++ string icu

在我的应用程序中,我使用ICU UnicodeString来存储我的字符串。由于我使用了一些与ICU不兼容的库,我需要将UnicodeString转换为其平台相关的表示。

基本上我需要做的是创建新的UnicodeString对象的逆过程表单 - 新的UnicodeString(“在系统语言环境中编码的字符串”)。

我发现了this主题 - 所以我知道可以使用stringstream来完成。

所以我的答案是,可以用其他更简单的方式完成,而不使用stringstream进行转换吗?

3 个答案:

答案 0 :(得分:1)

您可以将UnicodeString::extract()与代码页(或转换器)一起使用。实际上,为代码页传递NULL将使用ICU检测到的默认代码页。

答案 1 :(得分:1)

我用

std::string converted;
us.toUTF8String(converted);
我们是(ICU)UnicodeString

答案 2 :(得分:0)

您可以使用ucnv.h中的功能 - 即void ucnv_fromUnicode (UConverter *converter, char **target, const char *targetLimit, const UChar **source, const UChar *sourceLimit, int32_t *offsets, UBool flush, UErrorCode *err)。它不是一个很好的C ++ API,如UnicodeString,但它可以工作。

如果可能的话,我建议您坚持使用已经使用的operator<<。它是在任何情况下在C ++中处理词法转换(即字符串到/从整数的字符串)的标准方法。