我想实现程序,它将utf16文件转换为utf8,将utf8文件转换为utf16。这是我的代码: 我打开文件:
ifstream sourceFile(sourceFilePath, std::ifstream::binary);
ofstream outFile("/home/myuser/Desktop/res_file.txt");
This code converts utf8 to utf16 and IT WORKS:
vector<unsigned short> utf16line;
utf8::utf8to16(std::istreambuf_iterator<char>(sourceFile),
std::istreambuf_iterator<char>(),
back_inserter(utf16line));
outFile.write((char*)&utf16line[0], utf16line.size()*2);
但下一个代码是否工作:
vector<unsigned char> utf8result;
utf8::utf16to8(std::istreambuf_iterator<char>(sourceFile),
std::istreambuf_iterator<char>(),
back_inserter(utf8result));
outFile.write((char*)&utf8result[0], utf8result.size());
而不是获取正确的文件:
some string
12345
nnnnn
我有这个:
뿯뺿some string //chines symbols shouldn't be there
12345
nnnnn
从utf-16文件到utf-8文件的对话有什么问题?