std::string StringToUTF8(const std::string& str)
{
std::wstring ansiStr;
{
int nLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), NULL, 0);
WCHAR* buffer = new WCHAR[nLen + 1];
::ZeroMemory(buffer, sizeof(WCHAR)* (nLen + 1));
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), buffer, nLen);
ansiStr = buffer;
delete[] buffer;
}
std::string utf8Str;
{
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, ansiStr.c_str(), -1, NULL, 0, NULL, NULL);
char* buffer = new char[nLen + 1];
::ZeroMemory(buffer, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, ansiStr.c_str(), -1, buffer, nLen, NULL, NULL);
utf8Str = buffer;
delete[] buffer;
}
return utf8Str;
}
测试效果