char szBuf[1024] = "";
int nLength = recv(sockdownfile, szBuf, 1024, 0); // 接收数据。
经过跟踪szBuf的值为:字符串的内容如下(其中包括换行)。 
"HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
ETag: W/"529-1183443858000"
Last-Modified: Tue, 03 Jul 2007 06:24:18 GMT
Content-Type: text/plain
Content-Length: 529
Date: Tue, 03 Jul 2007 15:32:49 GMT
Connection: close<PARAMS>
<ITEMNUM"
// 接下来我就是想得到从<PARAMS>开始以后的字符串,把它前面的字符串去掉。
// 我的代码如下:
CString strBuf = szbuf;
int nIndex = strBuf.Find("\r\n\r\n");
if (nIndex != -1)
{
   int nLenszbuf = strBuf.GetLength();
   CString strXml = strBuf.Mid(nLenszbuf - nIndex);
}
经过跟踪,此时strXml的值为
"YEWU_TYPE><REPLY_TYPE>鐭俊</REPLY_TYPE><TITLE>t3</TITLE><CONTENT>c3</CONTENT></ITEM2>
<ITEM3><NUM>4</NUM><YEWU_TYPE>0</YEWU_TYPE><REPLY_TYPE>鐭俊</REPLY_TYPE><TITLE>t4</TITLE><CONTENT>c4</CONTENT></ITEM3>
</PARAMS>"
中间的汉字都变成了乱码,请问这是怎么回事?该怎么解决。

解决方案 »

  1.   

    这是我以前的一段代码,是把宽字符转换为普通字符后避免汉字乱码问题。/**
     * 宽字符转化为普通字符后写入流,如果直接写入宽字符,则汉字为乱码
    */
    std::wstring str = m_bstrHtml ? m_bstrHtml : L"";
    int nLen = WideCharToMultiByte( CP_ACP, 0, str.c_str(), (int)str.length(),NULL, 0, NULL, NULL );
    char *pchar =new char[ nLen+1 ];
    if( pchar )
    {
       memset( pchar,0,nLen+1 );
       WideCharToMultiByte( CP_ACP, 0, str.c_str(), (int)str.length(), pchar , nLen,   NULL, NULL );
       pStream->Write( pchar,ULONG(strlen(pchar)),NULL );
       delete []pchar;
    }
    我觉得你的问题是和我的相反,你应该把你的char szBuf转换为宽字符后然后在进行处理,也许就对了。
     楼主试试
      

  2.   

    因为汉字是由两个ASCII码组成的,而英文字母是由一个ASCII码组成的,
    看下面这个例子:
    ABCabc你好!
    如果你截取了前面7个字符,后面的就乱码了.我想你应该判断当前字母是不是属于汉字的前半部分,汉字的每一个字符通常都是负数(byte值)
      

  3.   

    因为你只截取了前半个,你做之前判断一下
    char temp[20];
    strcpy(temp,"方法123方法");
    if(temp[7]&0x80)取前7个,如果有半个汉字,覆盖
    {
       temp[7] = '\0';
    }
    else 
       temp[8] = '\0';
      

  4.   

    CString str /*= "12你好?/<"*/;while(!str.IsEmpty())
    {
    char ch = str.GetAt(0);
    int n = int(ch);
    CString str1;
    if (n > 128 || n < 0)
    {
    str1 = str.Left(2);
    str = str.Mid(2);
    }
    else
    {
    str1 = ch;
    str = str.Mid(1);
    }
    }