MFC开发IM-字符串转换大全

1.CString 转 LPCTSTR 

  CString cs_temp;

  LPCTSTR lpcstr=cs_temp;

2.char数组 转 CString

char* abc = szResponse;
CString temp(abc);

resJson = abc;

3.char * 转 CString

char* abc = szResponse;

CString temp(abc);

4.CString to char数组

    const char* temp  = CStringToConstChar(resJson);
char recv[50000];

strncpy_s(recv,temp,50000);

CStringToConstChar函数的实现在最下面


//*****以下为********CStringToConstChar函数的实现

 const char* mainDlg::CStringToConstChar(CString str){

 const char* res;
 //CString str(L"This is a test");
int len = WideCharToMultiByte( CP_UTF8 , 0 , str , str.GetLength() , NULL , 0 , NULL , NULL );
char* pAscii =new char[len+1];
len = WideCharToMultiByte(  CP_UTF8 , 0 , str , str.GetLength() , pAscii , len +1 , NULL ,NULL );

pAscii[len] = 0;

//const char* res = (const char*)pAscii;
res = (const char*)pAscii;

char a = res[0];
char b = res[1];
char c = res[2];
char d = res[3];
 return res;
 
 }

//******以上为*******CStringToConstChar函数的实现



猜你喜欢

转载自blog.csdn.net/golddaniu/article/details/79870182