1

我有一个wchar_t字符串,比如L"hao123--我的上网主页",可以转成utf8

编码,输出字符串是“hao123锛嶏紞鎴戠殑涓婄绣涓婚〉”,但最后还是要写这个

字符串转换为纯文本文件,其格式为 utf16(我从其他人那里知道),“hao123\uFF0D\uFF0D\u6211\u7684\u4E0A\u7F51\u4E3B\u9875”。

因为我必须将它保存在 C++ std 字符串中,然后将其写入文件,我该如何转换

“hao123锛嶏紞鎴戠殑涓婄绣涓婚>”到“hao123\uFF0D\uFF0D\u6211\u7684\u4E0A\u7F51\u4E3B\u9875”在char或C++标准字符串中?

谁能给我一些提示?

提前致谢!

4

2 回答 2

3

在 C++11 中,这可以通过以下方式完成:

#include <locale>
#include <codecvt>
#include <string>
#include <iostream>

int main()
{
    std::wstring_convert<std::codecvt_utf16<wchar_t> > myconv;
    std::wstring ws(L"hao123--\x6211\x7684\x4E0A\x7F51\x4E3B\x9875");
    std::string bs = myconv.to_bytes(ws);
    std::cout << bs << '\n';
}
于 2011-03-31T13:29:42.360 回答
0

转换函数是我自己写的,更多信息请访问C++ unicode UTF-16 encoding

于 2010-04-22T06:38:16.253 回答