Codifica predefinita attivata:
- Windows UTF-16.
- Linux UTF-8.
- MacOS UTF-8.
Questo codice ha due forme per convertire std :: string in std :: wstring e std :: wstring in std :: string. Se si annulla # se definito WIN32, si ottiene lo stesso risultato.
1. std :: stringa in std :: wstring
• MultiByteToWideChar WinAPI
• _mbstowcs_s_l
#if defined WIN32
#include <windows.h>
#endif
std::wstring StringToWideString(std::string str)
{
if (str.empty())
{
return std::wstring();
}
size_t len = str.length() + 1;
std::wstring ret = std::wstring(len, 0);
#if defined WIN32
int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, &str[0], str.size(), &ret[0], len);
ret.resize(size);
#else
size_t size = 0;
_locale_t lc = _create_locale(LC_ALL, "en_US.UTF-8");
errno_t retval = _mbstowcs_s_l(&size, &ret[0], len, &str[0], _TRUNCATE, lc);
_free_locale(lc);
ret.resize(size - 1);
#endif
return ret;
}
2. std :: wstring in std :: string
• WideCharToMultiByte WinAPI
• _wcstombs_s_l
std::string WidestringToString(std::wstring wstr)
{
if (wstr.empty())
{
return std::string();
}
#if defined WIN32
int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, &wstr[0], wstr.size(), NULL, 0, NULL, NULL);
std::string ret = std::string(size, 0);
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, &wstr[0], wstr.size(), &ret[0], size, NULL, NULL);
#else
size_t size = 0;
_locale_t lc = _create_locale(LC_ALL, "en_US.UTF-8");
errno_t err = _wcstombs_s_l(&size, NULL, 0, &wstr[0], _TRUNCATE, lc);
std::string ret = std::string(size, 0);
err = _wcstombs_s_l(&size, &ret[0], size, &wstr[0], _TRUNCATE, lc);
_free_locale(lc);
ret.resize(size - 1);
#endif
return ret;
}
3. Su Windows devi stampare Unicode, usando WinAPI.
• WriteConsole
#if defined _WIN32
void WriteLineUnicode(std::string s)
{
std::wstring unicode = StringToWideString(s);
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), unicode.c_str(), unicode.length(), NULL, NULL);
std::cout << std::endl;
}
void WriteUnicode(std::string s)
{
std::wstring unicode = StringToWideString(s);
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), unicode.c_str(), unicode.length(), NULL, NULL);
}
void WriteLineUnicode(std::wstring ws)
{
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), ws.c_str(), ws.length(), NULL, NULL);
std::cout << std::endl;
}
void WriteUnicode(std::wstring ws)
{
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), ws.c_str(), ws.length(), NULL, NULL);
}
4. Sul programma principale.
#if defined _WIN32
int wmain(int argc, WCHAR ** args)
#else
int main(int argc, CHAR ** args)
#endif
{
std::string source = u8"ÜüΩωЙ你月曜日\na🐕èéøÞǽлљΣæča🐕🐕";
std::wstring wsource = L"ÜüΩωЙ你月曜日\na🐕èéøÞǽлљΣæča🐕🐕";
WriteLineUnicode(L"@" + StringToWideString(source) + L"@");
WriteLineUnicode("@" + WidestringToString(wsource) + "@");
return EXIT_SUCCESS;
}
5. Infine, è necessario un supporto completo e potente per i caratteri Unicode nella console.
Raccomando ConEmu e impostarlo come terminale predefinito su Windows . È necessario collegare Visual Studio a ConEmu. Ricorda che il file exe di Visual Studio è devenv.exe
Testato su Visual Studio 2017 con VC ++; std = c ++ 17.
Risultato