用于字符串编码的 boost::locale to_utf 函数,喜欢抛出

Posted

技术标签:

【中文标题】用于字符串编码的 boost::locale to_utf 函数,喜欢抛出【英文标题】:boost::locale to_utf function for string encoding, likes to throw 【发布时间】:2012-06-16 14:04:18 【问题描述】:

我在使用 boost 在不同的字符串编码之间进行转换时遇到了问题。 After reading this,我试过这样做:

boost::locale::generator gen;
std::locale loc = gen.generate("");// encoding local to the computer.
//std::locale loc = gen.generate("en_US.UTF-8"); // tried this too
//std::locale loc = gen.generate("en_US.UTF-8"); //doesn't work either
std::string someString = "test me";
std::string output = boost::locale::conv::to_utf<char>(someString, loc);

但是当它在 to_utf 中尝试 use_facet() 时,它总是会失败并返回 std::bad_cast。

这表明正在生成语言环境的 boost locale::info facet isn't available。但是我尝试了几代不同的版本,但都没有运气,我希望系统默认始终可以正常工作,所以我一定是在其他地方做错了。

有什么想法吗?

【问题讨论】:

【参考方案1】:
std::locale loc("");
std::locale conv_loc = boost::locale::util::create_info(loc, loc.name());
std::string output = boost::locale::conv::to_utf<char>(someString, conv_loc);

【讨论】:

以上是关于用于字符串编码的 boost::locale to_utf 函数,喜欢抛出的主要内容,如果未能解决你的问题,请参考以下文章