首页 服务器 编程 必备知识 搜索引擎 圩日手册
站内搜索
最近浏览
推荐文章
热文排行

C++ URL 编码/解码


//URL 编码
std::string urlencode(std::string encode)
{
   std::string result;
   for(unsigned int i = 0; i< static_cast<unsigned int>(encode.length()); i++)
   {
    char ch = encode[i];
    if(ch == ' ')
    {
     result += '+';
    }else if(ch >= 'A' && ch <= 'Z'){
     result += ch;
    }else if(ch >= 'a' && ch <= 'z'){
     result += ch;
    }else if(ch >= '0' && ch <= '9'){
     result += ch;
    }else if(ch == '-' || ch == '-' || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\'' || ch == '(' || ch == ')' ){
     result += ch;
    }else{
     result += '%';
     result += iChoo::iconv::char_to_hex(ch);
    }
   }
return result;
}
//URL 解码
std::string urldecode(std::string decode)
{
   std::string result;
   for(unsigned int i = 0; i< static_cast<unsigned int>(decode.length()); i++)
   {
    switch(decode[i])
    {
    case '+':
     result += ' ';
    break;
    case '%':
     if(isxdigit(decode[i + 1]) && isxdigit(decode[i + 2]))
     {
      result += iChoo::iconv::hex_to_char(decode[i+1], decode[i+2]);
      i += 2;
     }else {
      result += '%';
     }
    break;
    default:
     result += decode[i];
    break;
    }
   }
return result;
}

[wangjy17908]
添加时间:2009-11-17
版权所有(C)2005-2015