私もこの問題に遭遇しました。ブーストロケールライブラリを使用して解決します
try
{
std::string utf8 = boost::locale::conv::utf_to_utf<char, short>(
(short*)wcontent.c_str(),
(short*)(wcontent.c_str() + wcontent.length()));
content = boost::locale::conv::from_utf(utf8, "ISO-8859-1");
}
catch (boost::locale::conv::conversion_error e)
{
std::cout << "Fail to convert from UTF-8 to " << toEncoding << "!" << std::endl;
break;
}
boost::locale::conv::utf_to_utf関数は、UTF-16LE でエンコードされたバッファーから UTF-8に変換しようとします。boost::locale::conv::from_utf関数は、でエンコードされたバッファーから変換しようとします。 UTF-8 から ANSI へ、エンコーディングが正しいことを確認してください (ここでは、Latin-1、ISO-8859-1 のエンコーディングを使用しています)。
もう 1 つの注意点は、Linux では std::wstring の長さが 4 バイトですが、Windows では std::wstring の長さが 2 バイトであるため、std::wstring を使用して UTF-16LE バッファーを含めることはお勧めしません。