字符串转换封装类
#include <memory> //定义Stl字符串类型,自动调用对应的类型 #ifdef UNICODE #define StlString std::wstring #else #define StlString std::string #endif class PLSimLocale { public: PLSimLocale(); ~PLSimLocale(); static void UTF8ToUnicode(const char* UTF8, std::wstring& unicodeString); static std::string WStringToString(const std::wstring& str); static std::wstring StringToWString(const std::string& str); static std::wstring CStringToWString(const CStringA& str); static StlString CStringAToStlString(const CStringA& str); static StlString CStringToStlString(const CString& str); static StlString StringToStlString(const std::string& str); static CStringA CStringWToCStringA(const CStringW& str); static CStringA CStringToCStringA(const CString& str); static std::string StlStringToString(const StlString& str); static std::wstring StlStringToWString(const StlString& str); static StlString WStringToStlString(const std::wstring& str); static std::string GetCurrentDateTime(std::string strFormat = "" ); };
#include "stdafx.h" #include "PLSimLocale.h" #include <sstream> PLSimLocale::PLSimLocale() { } PLSimLocale::~PLSimLocale() { } void PLSimLocale::UTF8ToUnicode(const char* UTF8, std::wstring& unicodeString) { DWORD dwUnicodeLen; //转换后Unicode的长度 WCHAR *pwText; //保存Unicode的指针 //获得转换后的长度,并分配内存 dwUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, UTF8, -1, NULL, 0); pwText = new WCHAR[dwUnicodeLen]; if (!pwText) { return; } //转为Unicode MultiByteToWideChar(CP_UTF8, 0, UTF8, -1, pwText, dwUnicodeLen); //转为CString unicodeString = pwText; //清除内存 delete[] pwText; //返回 return; } std::string PLSimLocale::WStringToString(const std::wstring& str) { unsigned int len = str.size() * 2 + 2; setlocale(LC_CTYPE, ""); char * p = new char[len]; size_t intlen; wcstombs_s(&intlen, p, len, str.c_str(), len); std::string str1(p); delete[] p; return str1; } std::wstring PLSimLocale::StringToWString(const std::string& str) { unsigned int len = str.size() + 1; setlocale(LC_CTYPE, ""); wchar_t *p = new wchar_t[len]; size_t intlen; mbstowcs_s(&intlen, p, len, str.c_str(), _TRUNCATE); std::wstring str1(p); delete[] p; return str1; } std::wstring PLSimLocale::CStringToWString(const CStringA& str) { unsigned int len = str.GetLength() + 1; setlocale(LC_CTYPE, ""); wchar_t *p = new wchar_t[len]; size_t intlen; mbstowcs_s(&intlen, p, len, str.GetString(), _TRUNCATE); std::wstring str1(p); delete[] p; return str1; } CStringA PLSimLocale::CStringToCStringA(const CString& str) { #ifdef UNICODE return CStringWToCStringA(str); #else return str; #endif } StlString PLSimLocale::CStringAToStlString(const CStringA& str) { #ifdef UNICODE return CStringToWString(str.GetString()); #else return StlString(str.GetString()); #endif } StlString PLSimLocale::StringToStlString(const std::string& str) { #ifdef UNICODE return StringToWString(str); #else return str; #endif } CStringA PLSimLocale::CStringWToCStringA(const CStringW & str) { int len = WideCharToMultiByte(CP_ACP, 0,str, -1, NULL, 0, NULL, NULL); std::shared_ptr<char> pstrLocal(new char[len], std::default_delete<char[]>()); memset(pstrLocal.get(), 0, len * sizeof(char)); WideCharToMultiByte(CP_ACP, 0, str, -1, pstrLocal.get(), len, NULL, NULL); CStringA cstr = pstrLocal.get(); return cstr; } std::string PLSimLocale::StlStringToString(const StlString& str) { #ifdef UNICODE return WStringToString(str); #else return str; #endif } std::wstring PLSimLocale::StlStringToWString(const StlString& str) { #ifdef UNICODE return str; #else return StringToWString(str); #endif } StlString PLSimLocale::WStringToStlString(const std::wstring& str) { #ifdef UNICODE return str; #else return WStringToString(str); #endif } StlString PLSimLocale::CStringToStlString(const CString& str) { return StlString(str.GetString()); } std::string PLSimLocale::GetCurrentDateTime(std::string strFormat) { SYSTEMTIME sys; GetLocalTime(&sys); char szTime[20] = { 0 }; if (strFormat.length() == 0 ) { strFormat = "%d%d%d%d%d%d%d"; } printf_s(szTime, sizeof(szTime) / sizeof(szTime[0]), strFormat, sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds); return szTime; } std::string PLSimDataTypeConverter::IntToString(int i) { std::ostringstream ostream; ostream << static_cast<int>(i) << std::endl; return ostream.str(); }
赌上我的人生为梦想,即使是臭名远扬,也要我的名字响彻天堂