vc2010中CString转换为char*

来源:互联网 发布:pc加拿大预测软件 编辑:程序博客网 时间:2024/06/06 05:25

如题,网上看到很多方法都进行了尝试。

1.buf = (LPSTR)(LPCTSTR)str;      ==>     buf 显示的是第一个字符

2.strcpy(pNumber,strNumber);      ==>    error C2664: 'strcpy' : cannot convert 
                                       parameter 2 from 'class CString' 
                                       to 'const char *'

3.sprintf(szTemp,"%s",strTemp);   ==>      buf 显示的还是第一个字符


4.memcpy(pBuff,(LPCSTR)strBuf,strBuf.GetAllocLength());
                                ==>       error C2440: 'type cast' : cannot convert
                                          from 'class CString' to 'const char *'

5.char *pBuff=strBuf.GetBuffer(0);   ==>    error C2440: 'initializing' : cannot convert 
                                           from 'unsigned short *' to 'char *'

以上不成功的原因为上述转换只哟在基于MBCS的工程可以,而在基于UNICODE的工程是不行的,CString会以UNICODE的形式来保存数据,强制类型转换只会返回第一个字符。

尝试成功的方法为:更改项目属性。

在vs2010下设置unicode编译选项,去掉unicode模式,具体设置方法为:

       中文方法:项目-》属性-》配置属性-》字符集-》未设置

      英文方法: project->Properties->Configuration Properties->General->Character Set->Not Set


0 0
原创粉丝点击