Windows下ANSI与UTF8之间转换,wstring与string之间的转换代码片段
参考:multiByteToWideChar 函数 (stringapiset.h) - Win32 apps | Microsoft Learn
static std::string UTF82ANSI(const char* utf8)
{std::string str;
#if _WIN32int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);if (len <= 0)return str;wchar_t* wstr = new wchar_t[len + 1];memset(wstr, 0, len + 1);MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);char* buf = new char[len + 1];memset(buf, 0, len + 1);WideCharToMultiByte(CP_ACP, 0, wstr, -1, buf, len, NULL, NULL);str.assign(buf, len - 1);delete[] wstr;delete[] buf;
#elsestr = utf8;
#endifreturn std::move(str);
}static std::string ANSI2UTF8(const char* gb2312)
{std::string str;
#if _WIN32int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);if (len <= 0)return str;wchar_t* wstr = new wchar_t[len + 1];memset(wstr, 0, len + 1);MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);char* buf = new char[len + 1];memset(buf, 0, len + 1);WideCharToMultiByte(CP_UTF8, 0, wstr, -1, buf, len, NULL, NULL);str.assign(buf, len - 1);delete[] wstr;delete[] buf;
#elsestr = gb2312;
#endifreturn std::move(str);
}
wstring与string直接转换
windows版
static std::wstring to_wide_string(std::string& strascii)
{
#ifdef _MSC_VERint widesize = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0); if (widesize == ERROR_NO_UNICODE_TRANSLATION){throw std::exception("Invalid UTF-8 sequence.");}if (widesize == 0){throw std::exception("Error in conversion.");}std::vector<wchar_t> resultstring(widesize);int convresult = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);if (convresult != widesize){throw std::exception("La falla!");}return std::wstring(&resultstring[0]);
#endif
}static std::string to_byte_string(const std::wstring& widestring)
{
#ifdef _MSC_VERint utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);if (utf8size == 0){throw std::exception("Error in conversion.");}std::vector<char> resultstring(utf8size);int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);if (convresult != utf8size){throw std::exception("La falla!");}return std::string(&resultstring[0]);
#endif
}
C++11版
#include <string>
#include <locale>
#include <codecvt>// convert string to wstring
inline std::wstring to_wide_string(const std::string& input)
{std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;return converter.from_bytes(input);
}
// convert wstring to string
inline std::string to_byte_string(const std::wstring& input)
{std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;return converter.to_bytes(input);
}