1、std::string 转 int
string valStr = "IconVisible";
int valInt = atoi(valStr.c_str());
2、std::string 转 char*
string valStr = "IconVisible";
const char* valStr_c = valStr.c_str();
3、int 转 string
int StartFilterTimes = 0;
string str = to_string(StartFilterTimes);
4、char* 转 string
const char * c = "test";
string str = (string)c;
5、char* 转 wchar*
wchar_t* Common::charToWChar(const char* c) {
const size_t cSize = strlen(c) + 1;
wchar_t* wc = new wchar_t[cSize];
mbstowcs(wc, c, cSize);
return wc;
}
6、wstring 转 string
std::string Common::WString2String(const std::wstring& ws)
{
std::string strLocale = setlocale(LC_ALL, "");
const wchar_t* wchSrc = ws.c_str();
size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
char* chDest = new char[nDestSize];
memset(chDest, 0, nDestSize);
wcstombs(chDest, wchSrc, nDestSize);
std::string strResult = chDest;
delete[]chDest;
setlocale(LC_ALL, strLocale.c_str());
return strResult;
}
7、string 转 LPCWSTR
LPCWSTR stringToLPCWSTR(std::string orig)
{
size_t origsize = orig.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t* wcstring = (wchar_t*)malloc(sizeof(wchar_t) * (orig.length() - 1));
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
return wcstring;
}