www.3112.net > mFC中Cstring转为ChAr

mFC中Cstring转为ChAr

原发布者:真燕无畏1、GetBuffer函数使用CString::GetBuffer函数.char*p;CStringstr="hello";p=str.GetBuffer(str.GetLength());str.ReleaseBuffer();将CString转换成char*时CStringstr("aaaaaaa");strcpy(str.GetBuffer(10),"aa");str.

CString str1 =_T("123"); int len =WideCharToMultiByte(CP_ACP,0,str1,-1,NULL,0,NULL,NULL); char *ptxtTemp =new char[len +1]; WideCharToMultiByte(CP_ACP,0,str1,-1,ptxtTemp,len,NULL,NULL ); delete[] ptxtTemp; //或者CString str;str = _T("123");CStringA str_a;str_a = str;char* a = str_a.GetBuffer(0);str_a.ReleaseBuffer();

char * cs2ca(CString str){ char *ptr; #ifdef _UNICODE LONG len; len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL); ptr = new char [len+1]; memset(ptr,0,len + 1); WideCharToMultiByte(CP_ACP, 0, str, -1, ptr, len + 1, NULL,

用包装类来转换,用法 type.parsetype(string); 原理:java中的基础数据类型可以互相自动或者强制转换(认为你会这个哦),然后string类是引用类型.和其他的类型不一样,就得用java为他们之间的转换设置的包装类,具体去看api. 你的这个就用:character.parsechar(string str).就可以转换了

微软有现成的转换函数,如下,在前面加个宏定义,uses_conversion;然后用宏转换t2a.cstring str = _t("中国人");uses_conversion;char* p = t2a(str.getbuffer(0));str.releasebuffer();

char * cs2ca(cstring str){ char *ptr; #ifdef _unicode long len; len = widechartomultibyte(cp_acp, 0, str, -1, null, 0, null, null); ptr = new char [len+1]; memset(ptr,0,len + 1); widechartomultibyte(cp_acp, 0, str, -1, ptr, len + 1, null, null); #else ptr = new char

当然不一样!p1和p2不是同一个指针,你比较的指针的地址!你应该用strcmp函数来比较字符串

如果你的工程是unicode版本的话 转成int: ;int a = _wtoi(str.getbuffer()); 转成double: double b = _wtof(str.getbuffer());

//cs是CString类型变量,只能转换为char*数组//VC6里面CString转char*:char *p =(LPSTR)(LPCSTR)cs;//VS里面CString转char*:int len=WideCharToMultiByte(CP_ACP,0,cs,-1,NULL,0,NULL,NULL);char *ptxtTemp =newchar[len +1];WideCharToMultiByte(CP_ACP,0,cs,-1,ptxtTemp,len,NULL,NULL);//相关处理,你可以把这段内容写成一个函数delete[] ptxtTemp;//记得释放内存

方法一:可以用API:WideCharToMultiByte进行转换方法二:可以添加文件#include <afxpriv.h>然后这样:CString strTest = _T("abcd");USES_CONVERSION;LPSTR = T2A(strTest);

网站地图

All rights reserved Powered by www.3112.net

copyright ©right 2010-2021。
www.3112.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com