#include <iostream>
#include <locale>
#include <string>
#include <codecvt>
#include <fstream>
std::string UnicodeToUTF8(const std::wstring & wstr)
{
//setlocale(LC_CTYPE, "");
std::string ret;
try {
std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
ret = wcv.to_bytes(wstr);
std::ofstream ofs("test.txt");
ofs<< ret;
for(int i = 0; i < ret.size(); i++){
printf("UnicodeToUTF8: %lu---%02X\n",ret.size(),(unsigned char)ret[i]);
}
} catch (const std::exception & e) {
std::cerr << e.what() << std::endl;
}
return ret;
}
int main ()
{
std::wstring str1 = L"你好";
std::string ret = UnicodeToUTF8(str1);
std::cout << ret << std::endl;
std::cout << "done!" << std::endl;
return 0;
}
上述代码在Linux下进行宽字节转UTF-8时运行 g++ -std=c++11 Test.cpp -o a.out进行编译时报 invalid or incomplete multibyte or wide character 错误,通过网上查资料,有说需要在主线程中添加 setlocale(LC_ALL, “”) ,添加后未能解决,最终发现是Test.cpp保存格式为 GBK 造成的,解决办法有两个:
办法一:更改保存格式为UTF-8;
方法二:g++ -std=c++11 Test.cpp -o a.out -finput-charset=GBK