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 @   txwtech  阅读(1104)  评论(6编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示