CString转换成char *字符串问题

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

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

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


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

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

直接转换在基于MBCS的工程可以,而在基于UNICODE的工程是不行的,CString会以UNICODE的形式来保存数据,强制类型转换只会返回第一个字符

你的工程应该是基于UNICODE的吧?
是的话直接强制转换是不行的,
方法一:
可以用API:WideCharToMultiByte进行转换
方法二:
可以添加文件#include <afxpriv.h>
然后这样:
CString strTest = _T("abcd");
USES_CONVERSION;
LPSTR = T2A(strTest);
应该可以的,你试试

posted @ 2014-01-07 13:27  FREE小宝  阅读(520)  评论(0编辑  收藏  举报