CString 转 string
#include <atlconv.h> USES_CONVERSION; dstStr=T2A(srcStr.GetBuffer(0));
string 转 CString
CString pChar2CStr(const char* pChar) { std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C"; setlocale(LC_ALL, "chs"); const char* _Source = pChar;//s.c_str(); size_t converted = 0; size_t _Dsize = strlen(pChar) + 1; //s.size() + 1; wchar_t *_Dest = new wchar_t[_Dsize]; wmemset(_Dest, 0, _Dsize); //mbstowcs_s(_Dest,_Source,_Dsize); mbstowcs_s(&converted, _Dest, _Dsize, _Source, _TRUNCATE); std::wstring result = _Dest; delete []_Dest; setlocale(LC_ALL, curLocale.c_str()); // CString csResult(result.c_str() ); return CString(result.c_str() ); }
CTIme转CString
CTime tmSCan = CTime::GetCurrentTime(); CString szTime = tmScan.Format("'%Y-%m-%d %H:%M:%S'");
long long 转 string
string lltoString(long long t) { std::string result; std::strstream ss; ss << t; ss >> result; return result; }
::PostMessage传递自定义消息,接收函数参数LPARAM
接收消息从CString 转到 LPARAM
发送: CString *str=new CString(strRet); ::PostMessage(pObj->m_hWnd,WM_SET_COMMONRESULT_TOWEB,0,(LPARAM)str); 接收: CString* ps = (CString*)lParam; if(ps !=nullptr) { //处理字符串 } if(ps != nullptr) { delete ps; ps=nullptr; }
Ascii转Unicode
wstring AsciiToUnicode(const string& str) { // 预算-缓冲区中宽字节的长度 int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0); // 给指向缓冲区的指针变量分配内存 wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen); // 开始向缓冲区转换字节 MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen); wstring ret_str = pUnicode; free(pUnicode); return ret_str; }
Unicode 转 Ascii
string UnicodeToAscii(const wstring& wstr) { // 预算-缓冲区中多字节的长度 int ansiiLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr); // 给指向缓冲区的指针变量分配内存 char *pAssii = (char*)malloc(sizeof(char)*ansiiLen); // 开始向缓冲区转换字节 WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr); string ret_str = pAssii; free(pAssii); return ret_str; }
CString 拼接字符串
CString strRet=_T(""); strRet.Format(_T("{\"retcode\":%d, \"nInfo\":["), lRet); strRet += _T("{\"iBegin\":\""); strRet += pChar2CStr(sStartTi.c_str()); strRet += _T("\",\"iEndTime\":\""); strRet += pChar2CStr(sEndTi.c_str()); strRet += "\"}"; strRet += "]}";
弹出选择路径窗口
CString FicowGetDirectory() { BROWSEINFO bi; char name[MAX_PATH]; ZeroMemory(&bi, sizeof(BROWSEINFO)); bi.hwndOwner = AfxGetMainWnd()->GetSafeHwnd(); bi.pszDisplayName = name; bi.lpszTitle = "选择文件夹目录"; bi.ulFlags = BIF_RETURNFSANCESTORS; LPITEMIDLIST idl = SHBrowseForFolder(&bi); if (idl == NULL) return ""; CString strDirectoryPath; SHGetPathFromIDList(idl, strDirectoryPath.GetBuffer(MAX_PATH)); strDirectoryPath.ReleaseBuffer(); if (strDirectoryPath.IsEmpty()) return ""; if (strDirectoryPath.Right(1) != "\\") strDirectoryPath += "\\"; return strDirectoryPath; }
Unicode转Utf8
string CPTTSignalingDemoDlg::UnicodeToUtf8(const wstring& wstr) { // 预算-缓冲区中多字节的长度 int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr); // 给指向缓冲区的指针变量分配内存 char *pAssii = (char*)malloc(sizeof(char)*ansiiLen); // 开始向缓冲区转换字节 WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr); string ret_str = pAssii; free(pAssii); return ret_str; }
Utf8转Unicode
wstring CPTTSignalingDemoDlg::Utf8ToUnicode(const string& str) { // 预算-缓冲区中宽字节的长度 int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0); // 给指向缓冲区的指针变量分配内存 wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen); // 开始向缓冲区转换字节 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, pUnicode, unicodeLen); wstring ret_str = pUnicode; free(pUnicode); return ret_str; }
Ascii转Unicode wstring AsciiToUnicode(const string& str) { // 预算-缓冲区中宽字节的长度 int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0); // 给指向缓冲区的指针变量分配内存 wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen); // 开始向缓冲区转换字节 MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen); wstring ret_str = pUnicode; free(pUnicode); return ret_str; }