如题。我在实现他们的转换时使用了WideCharToMultiByte()和MultiByteToWideChar()函数,但是发现在末尾处少了一些字符,很郁闷!我的转换的代码如下://转化为wchar_t
int nNumber=MultiByteToWideChar(CP_ACP,0,pResult,-1,NULL,0);//pResult就是char*的中英文字符串
wchar_t* pwText=new wchar_t[nNumber];
MultiByteToWideChar(CP_ACP,0,pResult,-1,pwText,nNumber);CString msg=pwText;nt nLength=WideCharToMultiByte(CP_OEMCP,NULL,msg,-1,NULL,0,NULL,false);
char* pcharBuf=new char[nLength];
WideCharToMultiByte(CP_OEMCP,NULL,msg,-1,pcharBuf,nLength,NULL,false);
发现在末尾处少了一些字节,由于是在实现文件的加解密,对少了的这些字节非常敏感!请指教啊!
int nNumber=MultiByteToWideChar(CP_ACP,0,pResult,-1,NULL,0);//pResult就是char*的中英文字符串
wchar_t* pwText=new wchar_t[nNumber];
MultiByteToWideChar(CP_ACP,0,pResult,-1,pwText,nNumber);CString msg=pwText;nt nLength=WideCharToMultiByte(CP_OEMCP,NULL,msg,-1,NULL,0,NULL,false);
char* pcharBuf=new char[nLength];
WideCharToMultiByte(CP_OEMCP,NULL,msg,-1,pcharBuf,nLength,NULL,false);
发现在末尾处少了一些字节,由于是在实现文件的加解密,对少了的这些字节非常敏感!请指教啊!
这种要实现完全的拷贝使用memcpy函数可以得到正解!!!
不要使用MultiByteToWideChar等。
第一步用memcpy将char*拷贝到wchar_t*中,直接赋值给CString
再用memcpy从CString直接拷贝到char*中。
memcpy(bufArray,msg.m_strMsg.GetBuffer(),msg.m_strMsg.GetLength()*2);
memcpy函数真是好用!
改为:
memcpy(bufArray,msg.m_strMsg.GetBuffer(),sizeof(bufArray));更合适,注意内存溢出
一不小心用到了。就WideCharToMultiByte吧。
char* buf = "Hello";
CString str(buf);
AfxMessageBox(str);CString -> char*
#include <altbase.h>
CString strText(_T("Hello"));
USES_CONVERSION;
char* buf = T2A(strText);
CString _s;
const char* s ="ddsfs";
_s.Format("%s",s);char* <-----CString
CString cstr = "ASDDSD";
char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
cstr.ReleaseBuffer();