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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM