如何在 c++ 中读取用 utf-8 编码的 java unicode 字节字符串



我有原型消息,它以字符串形式存储在 mongo 中

,语法如下
data.toByteString().toStringUtf8();

这只不过是编码为 utf8 的 unicode。

现在我正在尝试使用以下方法从 mongo 在 c++ 端读取相同的内容 -

std::wstring str(mongoData.get_utf8().value.to_string().begin(), mongoData.get_utf8().value.to_string().end());
String str1(boost::locale::conv::utf_to_utf<char>(str.c_str(), str.c_str()+str.size());

但是上面做,str1 给出了损坏的数据。

请帮助我做错了什么。谢谢。

只是在这里猜测mongoData.get_utf8().value.to_string()返回一个字符串。

这意味着开始和结束迭代器完全不相关,因为它们来自不同的字符串。

简单的解决方案是创建自己的字符串副本并从该副本中获取迭代器:

auto mongo_string = mongoData.get_utf8().value.to_string();
std::wstring str(mongo_string.begin(), mongo_string().end());

最新更新