zoukankan      html  css  js  c++  java
  • c++ 对汉字进行url编码(实验管用)

    CString urlEncode(CString s)
    {

     int len = s.GetLength();
     char *out = new char[len*9+1];
     memset(out , 0 , len*9+1);
     int i , j;
     int ch = 0 ;

     static char  myhex[0xFF+1][4];  //add by zhouzd 2008-10-06
     static bool isinital = false;

     if ( !isinital )
     {
      for ( i = 0 ; i <= 0xFF ; ++i )
      {
       myhex[i][0] = '%';
       sprintf( myhex[i]+1 , "%02X" , i );
      }
      isinital = true;
     }

     for (i = 0 , j = 0; i < len ; ++i )
     {
      ch = s.GetAt(i);

      //printf("%c\n" , s.GetAt(i) );

      if ('A' <= ch && ch <= 'Z')         // 'A'..'Z'
      {
       out[j++] = ch;
      }
      else if ('a' <= ch && ch <= 'z')    // 'a'..'z'
      {
       out[j++] = ch;
      }
      else if ('0' <= ch && ch <= '9')    // '0'..'9'
      {
       out[j++] = ch;
      }
      else if (ch == ' ')           // space
      {
       out[j++] = '+';
      }
      else if (ch == '-' || ch == '_'        // 不需要转化
       || ch == '.' || ch == '!'
       || ch == '~' || ch == '*'
       || ch == '\'' || ch == '('
       || ch == ')')
      {
       out[j++] = ch;
      }
      else if (ch <= 0x007f)     // ASCII控制字符
      {   
       strcat(out , myhex[ch]);
       j += 3;
      }
      else if (ch <= 0x07FF)         // 非ASCII <= 0x7FF
      {
       strcat(out , myhex[0xc0 | (ch >> 6)]);
       strcat(out , myhex[0x80 | (ch & 0x3F)]);
       j += 6;
      }
      else                       // 0x7FF < ch <= 0xFFFF
      {
       strcat(out , myhex[0xe0 | (ch >> 12)]);
       strcat(out , myhex[0x80 | ((ch >> 6) & 0x3F)]);
       strcat(out , myhex[0x80 | (ch & 0x3F)]);
       j += 9;
      }
     }
     out[j] = '\0';
     USES_CONVERSION;
     CString result = A2W(out);

     delete out;
     out = NULL;

     return result;
    }

    怎么查看服务器返回值?

     pHTTP->EndRequest(HSR_SYNC);
         
         char strBuff[1025] = {0};
    string strHtml; //是string 不是CString
    while ((pHTTP->Read((void*)strBuff, 1024)) > 0)
    {
    strHtml += strBuff;
    }
    // _cprintf("\n读取内容结束...");

    int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strHtml.c_str(), -1, NULL, 0);
    WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
    memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));

    MultiByteToWideChar(CP_UTF8,0,strHtml.c_str(),-1, pUnicode,unicodeLen);
    CString str(pUnicode); //这就是要的内容
    delete []pUnicode;

  • 相关阅读:
    android webview 使用以及一些异常处理及上传文件问题
    bootstrap 下拉菜单不显示的解决办法
    无损图片缩放
    startssl,免费的ssl证书申请及注意事项
    iis7.5
    vs2013 类名颜色显示黑色,无法修改
    "未能找到类型或命名空间名称",引用dll的时候出错
    由于 add 运算符中“Chinese_PRC_CI_AS”和“Chinese_PRC_CS_AS_WS”之间的排序规则冲突
    asp.net mvc int[] 和 string[] 自定义数组绑定
    windbg定位WEB性能瓶颈案例一则
  • 原文地址:https://www.cnblogs.com/zhwl/p/2784891.html
Copyright © 2011-2022 走看看