1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include "stdafx.h" #include "StringConvert.h"
wstring AsciiToUnicode(const string &str) { int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0); wchar_t *wstr = new wchar_t[sizeof(wchar_t) * len]; MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wstr, len); wstring destr = wstr; delete[] wstr; return destr; }
string AsciiToUtf8(const string &str) { wstring wstr = AsciiToUnicode(str); string destr = UnicodeToUtf8(wstr); return destr; }
string UnicodeToAscii(const wstring &wstr) { int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr); char *str = new char[sizeof(char) * len]; WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, str, len, nullptr, nullptr); string destr = str; delete[] str; return destr; }
string UnicodeToUtf8(const wstring &wstr) { int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr); char *str = new char[sizeof(char) * len]; WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, str, len, nullptr, nullptr); string destr = str; delete[] str; return destr; }
string Utf8ToAscii(const string &str) { wstring wstr = Utf8ToUnicode(str); string destr = UnicodeToAscii(wstr); return destr; }
wstring Utf8ToUnicode(const string &str) { int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0); wchar_t *wstr = new wchar_t[sizeof(wchar_t) * len]; MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wstr, len); wstring destr = wstr; delete[] wstr; return destr; }
|