vc++,MfC ,cstring与char相互转换知识

 

//mapName = mapString;//----------------------原始-
string mapName;

CString strtemp,strtemp2;
//char temp[XMAX_FILE_LENGTH + 1];
char temp[1024];

char print_internal_mapname_content[1024];

//数组可以赋值给cstring.   mapName=print_internal_mapname_content;
// 学习用cstring to chars,则需要写代码 cstringTochars(strtemp, temp);
//strcpy(print_internal_mapname_content,temp);数组之间用strcpy
//cstring 之间可以直接赋值 mapName = mapString;

 

vs2008测试

 1 void cstringTochars(CString str, char* chars)
 2 {
 3 #ifdef UNICODE
 4     int n = str.GetLength(); // n = 14, len = 18
 5     int len = WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), NULL, 0, NULL, NULL);
 6     WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), chars, len, NULL, NULL);
 7     chars[len+1] = '\0'; //多字节字符以'\0'结束
 8 #else
 9     strcpy(chars, str);
10 #endif
11 }

 

posted @ 2019-12-11 11:46  txwtech  阅读(1101)  评论(6编辑  收藏  举报