小智 6
使用PowerShell将编码更改为UTF-8:
powershell -Command "Get-Content PATH\temp.txt -Encoding Unicode | Set-Content -Encoding UTF8 PATH2\temp.txt"
Run Code Online (Sandbox Code Playgroud)
如果您安装了 MSYS2,则该iconv软件包(默认安装)允许您使用:
iconv -f utf-16le -t utf-8 <input.txt >output.txt
Run Code Online (Sandbox Code Playgroud)
#include <iconv.h>
wchar_t *src = ...; // or char16_t* on non-Windows platforms
int srclen = ...;
char *dst = ...;
int dstlen = ...;
iconv_t conv = iconv_open("UTF-8", "UTF-16");
iconv(conv, (char*)&src, &srclen, &dst, &dstlen);
iconv_close(conv);
Run Code Online (Sandbox Code Playgroud)
小智 5
我也遇到过这个问题,我通过使用boost locale库来解决它
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;
}
Run Code Online (Sandbox Code Playgroud)
该升压::区域:: CONV :: utf_to_utf功能尝试从由UTF-16LE编码成UTF-8缓冲区转换,该升压::区域:: CONV :: from_utf功能尝试从由一个编码转换缓冲区UTF-8到ANSI,确保编码正确(这里我使用Latin-1编码,ISO-8859-1).
另一个提示是,在Linux中,std :: wstring的长度为4个字节,但在Windows中,std :: wstring的长度为2个字节,因此最好不要使用std :: wstring来包含UTF-16LE缓冲区.
谢谢大家,这就是我如何解决“跨”Windows 和 Linux 需求的方法:
MinGW、 和MSYSlibiconv源码包libiconv通过编译MSYS。就是这样。