1、char->string
char cKey[MAX_PATH] = {“demo" };
string str;
str=cKey;
2、int->char
char cCurrentTime[MAX_PATH] = {0};
int nYear=2017;
sprintf_s(cCurrentTime,32, "%04d年",nYear);
3、CString->char、string
CString cstr=L"demo"
>>char *cUserName=CStringToChar(cstr);
string str=cUserName;
int len = str.GetLength();
int nBytes = WideCharToMultiByte(CP_ACP, 0, str, len, NULL, 0, NULL, NULL);
char* temp = new char[nBytes + 1];
memset(temp, 0, len + 1);
WideCharToMultiByte(CP_OEMCP, 0, str, len, temp, nBytes, NULL, NULL);
temp[nBytes] = 0;
return temp;
}
string TCHAR2String(TCHAR *STR)
{
int nLen = WideCharToMultiByte(CP_ACP, 0,STR, -1, NULL, 0, NULL, NULL);
char* chRtn =new char[nLen*sizeof(char)];
WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, nLen, NULL, NULL);
string str(chRtn);
return str;
}
6、char->wchar
{
DWORD dwNum=MultiByteToWideChar(CP_ACP, 0, cText, -1, NULL, 0);
wchar_t *pwText= new wchar_t[dwNum];
if (!pwText)
{
delete[]pwText;
}
MultiByteToWideChar(CP_ACP,0, cText, -1, pwText, dwNum);
}
7、string->wchar
{
size_t size= str.length();
}
string wchar2string(TCHAR *STR)
{
int iLen = WideCharToMultiByte(CP_ACP, 0,STR, -1, NULL, 0, NULL, NULL);
char* chRtn = new char[iLen * sizeof(char)];
WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);
std::string str(chRtn);
return str;
}