免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1196 | 回复: 0
打印 上一主题 下一主题

URL编解码(URLEncode,URLDecode) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-06 15:27 |只看该作者 |倒序浏览
#include "stdafx.h"

#include string>


using namespace std;


unsigned int utf8_decode( char *s, unsigned int *pi )

{

    unsigned int c;

    int i = *pi;

    /* one digit utf-8 */

    if ((s & 128)== 0 ) {

        c = (unsigned int) s;

        i += 1;

    } else if ((s & 224)== 192 ) { /* 110xxxxx & 111xxxxx == 110xxxxx */

        c = (( (unsigned int) s & 31 )  6) +

            ( (unsigned int) s[i+1] & 63 );

        i += 2;

    } else if ((s & 240)== 224 ) { /* 1110xxxx & 1111xxxx == 1110xxxx */

        c = ( ( (unsigned int) s & 15 )  12 ) +

            ( ( (unsigned int) s[i+1] & 63 )  6 ) +

            ( (unsigned int) s[i+2] & 63 );

        i += 3;

    } else if ((s & 248)== 240 ) { /* 11110xxx & 11111xxx == 11110xxx */

        c =  ( ( (unsigned int) s & 7 )  18 ) +

            ( ( (unsigned int) s[i+1] & 63 )  12 ) +

            ( ( (unsigned int) s[i+2] & 63 )  6 ) +

            ( (unsigned int) s[i+3] & 63 );

        i+= 4;

    } else if ((s & 252)== 248 ) { /* 111110xx & 111111xx == 111110xx */

        c = ( ( (unsigned int) s & 3 )  24 ) +

            ( ( (unsigned int) s[i+1] & 63 )  18 ) +

            ( ( (unsigned int) s[i+2] & 63 )  12 ) +

            ( ( (unsigned int) s[i+3] & 63 )  6 ) +

            ( (unsigned int) s[i+4] & 63 );

        i += 5;

    } else if ((s & 254)== 252 ) { /* 1111110x & 1111111x == 1111110x */

        c = ( ( (unsigned int) s & 1 )  30 ) +

            ( ( (unsigned int) s[i+1] & 63 )  24 ) +

            ( ( (unsigned int) s[i+2] & 63 )  18 ) +

            ( ( (unsigned int) s[i+3] & 63 )  12 ) +

            ( ( (unsigned int) s[i+4] & 63 )  6 ) +

            ( (unsigned int) s[i+5] & 63 );

        i += 6;

    } else {

        c = '?';

        i++;

    }

    *pi = i;

    return c;

}


std::string UrlEncode(const std::string& src)

{

    static    char hex[] = "0123456789ABCDEF";

    std::string dst;

   

    for (size_t i = 0; i  src.size(); i++)

    {

        unsigned char ch = src;

        if (isalnum(ch))

        {

            dst += ch;

        }

        else

            if (src == ' ')

            {

                dst += '+';

            }

            else

            {

                unsigned char c = static_castunsigned char>(src);

                dst += '%';

                dst += hex[c / 16];

                dst += hex[c % 16];

            }

    }

    return dst;

}


std::string UrlDecode(const std::string& src)

{

    std::string dst, dsturl;


    int srclen = src.size();


    for (size_t i = 0; i  srclen; i++)

    {

        if (src == '%')

        {

            if(isxdigit(src[i + 1]) && isxdigit(src[i + 2]))

            {

                char c1 = src[++i];

                char c2 = src[++i];

                c1 = c1 - 48 - ((c1 >= 'A') ? 7 : 0) - ((c1 >= 'a') ? 32 : 0);

                c2 = c2 - 48 - ((c2 >= 'A') ? 7 : 0) - ((c2 >= 'a') ? 32 : 0);

                dst += (unsigned char)(c1 * 16 + c2);

            }

        }

        else

            if (src == '+')

            {

                dst += ' ';

            }

            else

            {

                dst += src;

            }

    }


    int len = dst.size();

   

    for(unsigned int pos = 0; pos  len;)

    {

        unsigned int nvalue = utf8_decode((char *)dst.c_str(), &pos);

        dsturl += (unsigned char)nvalue;

    }


    return dsturl;

}


// 测试程序


int main(int argc, char* argv[])

{

    string str1 = "VC知识库 vckbase.com";

    string str2 = "www.vckbase.com/sql.asp?id=2%20update and sele%%ct%fc%80%80%80%80%af";


    printf("%s ", UrlEncode(str1).c_str());    // URL编码

    printf("%s ", UrlDecode(str2).c_str());    // URL解码

   

    return 0;

}

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/64804/showart_1777567.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP