如题。我在实现他们的转换时使用了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);
发现在末尾处少了一些字节,由于是在实现文件的加解密,对少了的这些字节非常敏感!请指教啊!

解决方案 »

  1.   

    我自己找到答案了!花费了一个上午,郁闷!
    这种要实现完全的拷贝使用memcpy函数可以得到正解!!!
    不要使用MultiByteToWideChar等。
    第一步用memcpy将char*拷贝到wchar_t*中,直接赋值给CString
    再用memcpy从CString直接拷贝到char*中。
    memcpy(bufArray,msg.m_strMsg.GetBuffer(),msg.m_strMsg.GetLength()*2);
    memcpy函数真是好用!
      

  2.   

    memcpy(bufArray,msg.m_strMsg.GetBuffer(),msg.m_strMsg.GetLength()*2);
    改为:
    memcpy(bufArray,msg.m_strMsg.GetBuffer(),sizeof(bufArray));更合适,注意内存溢出
      

  3.   

    如果你是一个UNICODE的工程的话,CString就OK了,就不要再用char*了。
    一不小心用到了。就WideCharToMultiByte吧。
      

  4.   

    char* -> CString
    char* buf = "Hello";
    CString str(buf);
    AfxMessageBox(str);CString -> char*
    #include <altbase.h>
    CString strText(_T("Hello"));
    USES_CONVERSION;
    char* buf = T2A(strText);
      

  5.   

    char* -->CString
    CString _s;
    const char* s ="ddsfs";
    _s.Format("%s",s);char* <-----CString
    CString cstr = "ASDDSD";
    char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
    cstr.ReleaseBuffer();
      

  6.   

    我这里是在MFC、Unicode下的情况。由于要使用DLL,DLL是C语言编写的,所以转化头痛
      

  7.   

    如果是读取数据库的中文字符的话。linux 还需要考虑oracle 的环境变量 $NLS_LANG='SIMPLIFIED CHINESE_CHINA.ZHS16GBK' 这样读取中文转换成UTF8才有效。