unicode下类型转化

来源:互联网 发布:互普威盾阻止软件安装 编辑:程序博客网 时间:2024/05/16 06:31

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;

char* CStringToChar(CString &str)
{
 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;
}


>>char *cUserName = CStringA(cstr).GetBuffer(0);

4、Cstring->int

CStrign str("test");
int i=_ttoi(str);

5、TCHAR->string

·TCHAR>>char*
·char*>>string

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

wchar_t * char2wchar(char* cText)
{
 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);
 return pwText;
}


7、string->wchar

wchar_t * string2wchar(string str)
{
 size_t size= str.length();
 wchar_t* buffer= new wchar_t[size + 1];
 MultiByteToWideChar(CP_ACP, 0, str.c_str(), size, buffer, size*sizeof(wchar_t));
 buffer[size] = 0;
 return buffer;
}


8、 wchar->string

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;
}

原创粉丝点击