精简化:
//通用版将wstring转化为string __inline static std::string WCToMB(const std::wstring& ws, unsigned long cp = CP_ACP) { if (ws.empty() == true)return (""); std::string s(WideCharToMultiByte(cp, 0, ws.data(), ws.size(), NULL, 0, NULL, NULL), ('\0')); return s.substr(0, WideCharToMultiByte(cp, 0, ws.c_str(), ws.size(), (LPSTR)s.data(), (int)s.size(), NULL, NULL)); } //通用版将string转化为wstring __inline static std::wstring MBToWC(const std::string& s, unsigned long cp = CP_ACP) { if (s.empty() == true)return (L""); std::wstring ws(MultiByteToWideChar(cp, 0, s.data(), s.size(), NULL, 0), (L'\0')); return ws.substr(0, MultiByteToWideChar(cp, 0, s.data(), s.size(), (LPWSTR)ws.data(), (int)ws.size())); } #define ANSIToWIDE(A) MBToWC(A,CP_ACP) #define WIDEToANSI(W) WCToMB(W,CP_ACP) #define UTF8ToWIDE(U) MBToWC(U,CP_UTF8) #define WIDEToUTF8(W) WCToMB(W,CP_UTF8) #define ANSIToUTF8(A) WIDEToUTF8(ANSIToWIDE(A)) #define UTF8ToANSI(U) WIDEToANSI(UTF8ToWIDE(U))
转换类:
class ConsoleDebugUtils { private: FILE* pFIN; FILE* pFOUT; FILE* pFERR; public: //初始化调试窗口显示 void InitDebugConsoleA() { AllocConsole(); { SetConsoleTitleA(("TraceDebugWindow")); pFIN = freopen(("CONIN$"), ("rb"), stdin); pFOUT = freopen(("CONOUT$"), ("wb"), stdout); pFERR = freopen(("CONOUT$"), ("wb"), stderr); setlocale(LC_ALL, ("chs")); } } //初始化调试窗口显示 void InitDebugConsoleW() { AllocConsole(); { SetConsoleTitleW((L"TraceDebugWindow")); pFIN = _wfreopen((L"CONIN$"), (L"rb"), stdin); pFOUT = _wfreopen((L"CONOUT$"), (L"wb"), stdout); pFERR = _wfreopen((L"CONOUT$"), (L"wb"), stderr); _wsetlocale(LC_ALL, (L"chs")); } } //初始化调试窗口显示 void InitDebugConsole() { if (!AllocConsole()) { SetConsoleTitle(TEXT("TraceDebugWindow")); pFIN = _tfreopen(TEXT("CONIN$"), TEXT("rb"), stdin); pFOUT = _tfreopen(TEXT("CONOUT$"), TEXT("wb"), stdout); pFERR = _tfreopen(TEXT("CONOUT$"), TEXT("wb"), stderr); _tsetlocale(LC_ALL, TEXT("chs")); } } //释放掉调试窗口显示 void ExitDebugConsole() { if (pFERR) { fclose(pFERR); pFERR = NULL; } if (pFOUT) { fclose(pFOUT); pFOUT = NULL; } if (pFIN) { fclose(pFIN); pFIN = NULL; } FreeConsole(); } public: static ConsoleDebugUtils* Instance() { static ConsoleDebugUtils cduInstance; return &cduInstance; } }; class StringConvertUtils { #if !defined(_UNICODE) && !defined(UNICODE) #define tstring std::string #else #define tstring std::wstring #endif public: // ANSI to Unicode std::wstring AToW(const std::string str) { int len = 0; len = str.length(); int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t* pUnicode; pUnicode = new wchar_t[(unicodeLen + 1)]; memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t)); ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen); std::wstring rt; rt = (wchar_t*)pUnicode; delete pUnicode; return rt; } //Unicode to ANSI std::string WToA(const std::wstring str) { char* pElementText; int iTextLen; iTextLen = WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL); pElementText = new char[iTextLen + 1]; memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1)); ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL); std::string strText; strText = pElementText; delete[] pElementText; return strText; } //UTF-8 to Unicode std::wstring UTF8ToW(const std::string str) { int len = 0; len = str.length(); int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); wchar_t* pUnicode; pUnicode = new wchar_t[unicodeLen + 1]; memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t)); ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen); std::wstring rt; rt = (wchar_t*)pUnicode; delete pUnicode; return rt; } //Unicode to UTF-8 std::string WToUTF8(const std::wstring str) { char* pElementText; int iTextLen; iTextLen = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL); pElementText = new char[iTextLen + 1]; memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1)); ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL); std::string strText; strText = pElementText; delete[] pElementText; return strText; } std::string TToA(tstring tsT) { std::string str = ""; #if !defined(UNICODE) && !defined(_UNICODE) str = tsT; #else str = WToA(tsT); #endif return str; } std::wstring TToW(tstring tsT) { std::wstring wstr = L""; #if !defined(UNICODE) && !defined(_UNICODE) wstr = AToW(tsT); #else wstr = tsT; #endif return wstr; } tstring AToT(std::string str) { tstring ts = TEXT(""); #if !defined(UNICODE) && !defined(_UNICODE) ts = str; #else ts = AToW(str); #endif return ts; } tstring WToT(std::wstring wstr) { tstring ts = TEXT(""); #if !defined(UNICODE) && !defined(_UNICODE) ts = WToA(wstr); #else ts = wstr; #endif return ts; } public: static StringConvertUtils* Instance() { static StringConvertUtils scuInstance; return &scuInstance; } };
纯C++11实现:
#include <codecvt> //这里的new无须手动释放,在wstring_convert释放时也会自动释放掉new的对象实例 //#define AToW_EX(s,l) std::wstring_convert<std::codecvt<wchar_t, char, std::mbstate_t>>(new std::codecvt<wchar_t, char, std::mbstate_t>(l)).from_bytes(s) //#define WToA_EX(s,l) std::wstring_convert<std::codecvt<wchar_t, char, std::mbstate_t>>(new std::codecvt<wchar_t, char, std::mbstate_t>(l)).to_bytes(s) #define AToW_EX(s,l) std::wstring_convert<std::codecvt_byname<wchar_t, char, std::mbstate_t>>(new std::codecvt_byname<wchar_t, char, std::mbstate_t>(l)).from_bytes(s) #define WToA_EX(s,l) std::wstring_convert<std::codecvt_byname<wchar_t, char, std::mbstate_t>>(new std::codecvt_byname<wchar_t, char, std::mbstate_t>(l)).to_bytes(s) #define AToW(s) AToW_EX(s,"chs") #define WToA(s) WToA_EX(s,"chs") #define WToUTF8(s) std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(s) #define UTF8ToW(s) std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(s) #if !defined(_UNICODE) && !defined(UNICODE) #define AToT_EX(s,l) WToA_EX(AToW_EX(s,l),l) #define TToA_EX(s,l) WToA_EX(AToW_EX(s,l),l) #define WToT_EX(s,l) WToA_EX(s,l) #define TToW_EX(s,l) AToW_EX(s,l) #define AToT(s) s #define TToA(s) s #define WToT(s) WToA(s) #define TToW(s) AToW(s) #define TToUTF8(s) WToUTF8(AToW(s)) #define AToUTF8(s) WToUTF8(AToW(s)) #define UTF8ToA(s) WToA(UTF8ToW(s)) #define UTF8ToT(s) WToA(UTF8ToW(s)) #else #define AToT_EX(s,l) AToW_EX(s,l) #define TToA_EX(s,l) WToA_EX(s,l) #define WToT_EX(s,l) AToW_EX(WToA_EX(s,l),l) #define TToW_EX(s,l) AToW_EX(WToA_EX(s,l),l) #define AToT(s) AToW(s) #define TToA(s) WToA(s) #define WToT(s) s #define TToW(s) s #define TToUTF8(s) WToUTF8(s) #define AToUTF8(s) WToUTF8(AToW(s)) #define UTF8ToA(s) WToA(UTF8ToW(s)) #define UTF8ToT(s) UTF8ToW(s) #endif //通用版将wstring转化为string __inline std::string W_To_A(const std::wstring& ws, unsigned int cp = CP_ACP) { if (!ws.empty()) { std::string s(WideCharToMultiByte(cp, 0, ws.data(), -1, NULL, 0, NULL, NULL), ('\0')); return s.substr(0, WideCharToMultiByte(cp, 0, ws.c_str(), -1, (LPSTR)s.data(), (int)s.size(), NULL, NULL) - 1); } return (""); } //通用版将string转化为wstring __inline std::wstring A_To_W(const std::string& s, unsigned int cp = CP_ACP) { if (!s.empty()) { std::wstring ws(MultiByteToWideChar(cp, 0, s.data(), -1, NULL, 0), (L'\0')); return ws.substr(0, MultiByteToWideChar(cp, 0, s.data(), -1, (LPWSTR)ws.data(), (int)ws.size()) - 1); } return (L""); } __inline static #if !defined(UNICODE) && !defined(_UNICODE) std::string #else std::wstring #endif A_To_T(const std::string& str) { #if !defined(UNICODE) && !defined(_UNICODE) return str; #else return A_To_W(str); #endif } __inline static #if !defined(UNICODE) && !defined(_UNICODE) std::string #else std::wstring #endif W_To_T(const std::wstring& wstr) { #if !defined(UNICODE) && !defined(_UNICODE) return W_To_A(wstr); #else return wstr; #endif } __inline static std::string T_To_A( const #if !defined(UNICODE) && !defined(_UNICODE) std::string #else std::wstring #endif & tsT) { #if !defined(UNICODE) && !defined(_UNICODE) return tsT; #else return W_To_A(tsT); #endif } __inline static std::wstring T_To_W( const #if !defined(UNICODE) && !defined(_UNICODE) std::string #else std::wstring #endif & tsT) { #if !defined(UNICODE) && !defined(_UNICODE) return A_To_W(tsT); #else return tsT; #endif } #define WToUTF8(X) W_To_A(X, CP_UTF8) #define UTF8ToW(X) A_To_W(X, CP_UTF8) #define AToUTF8(X) WToUTF8(A_To_W(X)) #define UTF8ToA(X) W_To_A(UTF8ToW(X)) //将From编码转化为To编码 __inline static std::string CodePage_FromTo(const std::string& str, unsigned int from_codepage, unsigned int to_codepage) { return W_To_A(A_To_W(str, from_codepage), to_codepage); } //将UTF8转化为ANSI __inline static std::string UTF8ToANSI(const std::string& str) { return CodePage_FromTo(str, CP_UTF8, CP_ACP); } //将ANSI转化为UTF8 __inline static std::string ANSIToUTF8(const std::string& str) { return CodePage_FromTo(str, CP_ACP, CP_UTF8); }
收藏的用户(0)
X
正在加载信息~
2
最新回复 (0)