CString strOld;
CString strNew;
GetDlgItem(IDC_EDIT_RECVDATA)->GetWindowText(strOld);
strNew.Format(_T("%s 说 %s"),inet_ntoa(addrRecv.sin_addr),wsabuf.buf);
strNew += "\r\n";
strNew += strOld;
GetDlgItem(IDC_EDIT_RECVDATA)->SetWindowText(strNew);MFC 在 CEdit 编辑框中显示乱码
原文应该是    127.0.0.1  说  Hello
现在变成了    ㈱⸷⸰⸰1멸몭몭몭몭몭 说 Hel췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍﷽﷽ꮫꮫꮫꮫﻮﻮ要如何解决,Thanks

解决方案 »

  1.   

    inet_ntoa返回的是char指针,你要把char转为WCHAR,网上有很多这方面的资料
      

  2.   

    CString strOld=NULL; 
    CString strNew=NULL; 
    GetDlgItem(IDC_EDIT_RECVDATA)->GetWindowText(strOld); 
    strNew.Format(_T("%s 说 %s"),_T(inet_ntoa(addrRecv.sin_addr)),_T(wsabuf.buf)); 
    strNew += "\r\n"; 
    strNew += strOld; 
      

  3.   

    size_t i = MultiByteToWideChar(CP_ACP,0,(const char *)wsabuf.buf,sizeof(wsabuf.buf),NULL,0);
    wchar_t *buffer = new wchar_t[i+1];
    MultiByteToWideChar(CP_ACP,0,(const char *)wsabuf.buf,sizeof(wsabuf.buf),buffer,i);为什么 buffer 中只有一个字符
    如 wsabuf.buf = “hello”
       buffer 中只有 'h'   一个字符
      

  4.   


    wide char对asc字符的编码是“h0e0l0l0o0"
    而C++默认将0当作字符串结尾看待实际上buffer中是“h0e0l0l0o0"
      

  5.   

    你最简单的做法是“项目”-->“属性”-->“配置属性”-->“字符集”-->“使用多字节字符集”不要用unicode
      

  6.   


    typedef struct __WSABUF 
    {  u_long len;  
    char FAR* buf;
    } WSABUF,  *LPWSABUF;让我们看看,wsabuf.buf的类型。
    呵呵,它是个char FAR*.很显然它应该是个字符缓冲,通常的我们可以认为它是个字节流。并非一个字符串。
    所以这个缓冲有可能存放的是unicode的编码数据,也可能是mcbs的数据。从楼主的需求看是想把ansi的"hello",转换成unicode的L"hello". 保存到buffer中// Convert ANSI to Unicode
    _tsetlocale(LC_ALL, _T(""));//第一步取得需要转换的字符的长度
    unsigned long lLen = wsabuf.len;//第二步取得转换成WCHAR,所需要的字符数
    int cch = MultiByteToWideChar(CP_ACP,0, wsabuf.buf, lLen, NULL, 0); //第三步定义缓存并清零
    WCHAR * wszBuffer = new WCHAR[cch + 1]; 
    ZeroMemory((char *)wszBuffer, (cch+1)*sizeof(wszBuffer[0]));//第四步转换
    MultiByteToWideChar( CP_ACP, 0, wsabuf.buf, lLen + 1, wszBuffer, cch);//输出
    TRACE(L"%s\n", wszBuffer);//释放
    delete [] wszBuffer;
      

  7.   

    strNew += "\r\n"; 
    这一行怎么没写上_T("");改成
    strNew += _T("\r\n");