zoukankan      html  css  js  c++  java
  • urlEncode和urlDecode

    绝对不编码的,仅仅有字母、数字、短横线(-)、下划线(_)、点(.)和波浪号(~),其它字符要视情况而定。所以一般性的urlencode仅仅需保留上述字符不进行编码。

    /** @file  url.cpp
    *  @note   
    *  @brief
    *  @author 
    *  @date   2019/10/07
    *  @note   
    *  @history
    *  @warning
    */
    #include <iostream>
    #include <assert.h>
    #include <string>
    using namespace std;
    unsigned char ToHex(unsigned char x)
    {
        return  x > 9 ? x + 55 : x + 48;
    }
    
    unsigned char FromHex(unsigned char x)
    {
        unsigned char y;
        if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
        else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
        else if (x >= '0' && x <= '9') y = x - '0';
        else assert(0);
        return y;
    }
    
    std::string UrlEncode(const std::string& str)
    {
        std::string strTemp = "";
        size_t length = str.length();
        for (size_t i = 0; i < length; i++)
        {
            if (isalnum((unsigned char)str[i]) ||
                (str[i] == '-') ||
                (str[i] == '_') ||
                (str[i] == '.') ||
                (str[i] == '~'))
                strTemp += str[i];
            else if (str[i] == ' ')
                strTemp += "+";
            else
            {
                strTemp += '%';
                strTemp += ToHex((unsigned char)str[i] >> 4);
                strTemp += ToHex((unsigned char)str[i] % 16);
            }
        }
        return strTemp;
    }
    
    std::string UrlDecode(const std::string& str)
    {
        std::string strTemp = "";
        size_t length = str.length();
        for (size_t i = 0; i < length; i++)
        {
            if (str[i] == '+') strTemp += ' ';
            else if (str[i] == '%')
            {
                assert(i + 2 < length);
                unsigned char high = FromHex((unsigned char)str[++i]);
                unsigned char low = FromHex((unsigned char)str[++i]);
                strTemp += high*16 + low;
            }
            else strTemp += str[i];
        }
        return strTemp;
    }
  • 相关阅读:
    echarts设置y轴线的样式
    echarts基础配置信息?
    提高网站用户体验使网站更好发展的五大要点
    没有或很少有出站链接的网站存在致命的缺陷
    网络推广之百度知道推广技巧
    如何让百度3分钟内收录你的文章
    CSS强制换行
    如何提高网站在搜索引擎中的权重
    js闭包深入详解
    深入浅出之正则表达式
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/11630773.html
Copyright © 2011-2022 走看看