王骏的BLOG
编程、网络技术点滴...
<
2008年9月
>
日
一
二
三
四
五
六
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
公告
留言簿(24)
给我留言
查看公开留言
查看私人留言
随笔分类
数据库 (3)
(rss)
C++,VC (11)
(rss)
.NET (14)
(rss)
烂笔头备忘录 (11)
(rss)
Web开发 (6)
(rss)
网络管理 (5)
(rss)
疑难杂症备忘 (9)
(rss)
随笔档案
2008年11月 (1)
2008年10月 (2)
2008年8月 (1)
2008年6月 (1)
2008年5月 (1)
2008年4月 (1)
2008年3月 (3)
2008年2月 (1)
2008年1月 (3)
2007年12月 (5)
2007年11月 (2)
2007年6月 (1)
2007年2月 (2)
2007年1月 (1)
2006年10月 (3)
2006年8月 (1)
2006年2月 (3)
2006年1月 (1)
2005年12月 (2)
2005年11月 (5)
2005年10月 (3)
2005年9月 (1)
2005年7月 (3)
2005年6月 (2)
2005年5月 (4)
2005年3月 (2)
2005年2月 (3)
2004年5月 (2)
2004年4月 (1)
文章分类
WEB开发 (2)
(rss)
网络技术 (3)
(rss)
文章档案
2006年11月 (1)
2005年11月 (1)
2005年4月 (1)
2004年10月 (1)
2004年9月 (1)
相册
程序员娱乐 (2)
WEB开发
在线CSS词典
相关链接
VC知识库博客园地
(rss)
VC知识库首页
宝宝的主页
搜索
最新评论
1. re: URL编解码(URLEncode,URLDecode)
多谢多谢!!!!
--KEN
2. re: IE无法打印一例
早上遇到有人的IE7(vista sp1)无法打印,他的临时文件在D盘,打印时说找不到文件。
解决办法:找到D盘上的临时文件夹,赋上对应的权限即可.
heron.96 AT Gm ail dot com
--VISTA
3. 中国美甲技术
[url=
http://www.coolestgirl.com.cn
]美甲[/url] 美甲
--零花钱
4. re: string与线程安全
--http://www.axdat.com
5. re: Sasser(震荡波)蠕虫病毒肆虐
不错不错,学习了。
--硬盘数据恢复
6. re: ASP.NET大文件上传
蛮热闹的嘛。..我也来踩踩
--上海国珍松花粉
7. re: ASP.NET大文件上传
支持一下,祝楼主越办越好!
--raid服务器数据恢复
8. re: .Text 使用经验:dotText的编译
好帖子,收藏了。
--硬盘数据恢复
9. re: URL编解码(URLEncode,URLDecode)
哈,太好了,正找真呢,谢谢。
--数据恢复
10. re: WINDOWS启动时自动关闭EXPLORER并使IE最大化
不错,学习了。
--上海数据恢复
阅读排行榜
1. .Text 使用经验:dotText的编译(11678)
2. ASP.NET大文件上传(10114)
3. 利用Visual Assist X的模板功能提高编辑效率(7365)
4. IE无法打印一例(6015)
5. [烂笔头备忘录] 删除list元素(4152)
6. Sasser(震荡波)蠕虫病毒肆虐(3977)
7. string与线程安全(3976)
8. [HTTP 500 - 内部主机错误]的处理(3915)
9. 双SCSI硬盘安装小结(3453)
10. 将VC知识库.Text Blog 从.95升级到.96(3105)
评论排行榜
1. ASP.NET大文件上传(56)
2. Sasser(震荡波)蠕虫病毒肆虐(48)
3. string与线程安全(21)
4. IE无法打印一例(17)
5. .Text 使用经验:dotText的编译(16)
6. .NET中的3DES加密 (13)
7. 传奇游戏服务器源码学习(13)
8. 将VC知识库.Text Blog 从.95升级到.96(11)
9. XMLHTTP与客户端无刷新更新数据(11)
10. [烂笔头备忘录] 删除list元素(10)
VC知识库BLOG
首页
新随笔
联系
聚合
登录
随笔-61 文章-5 评论-246 Trackbacks-0
URL编解码(URLEncode,URLDecode)
#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[i]
&
128
)
==
0
) {
c
=
(unsigned
int
) s[i];
i
+=
1
;
}
else
if
((s[i]
&
224
)
==
192
) {
/*
110xxxxx & 111xxxxx == 110xxxxx
*/
c
=
(( (unsigned
int
) s[i]
&
31
)
<<
6
)
+
( (unsigned
int
) s[i
+
1
]
&
63
);
i
+=
2
;
}
else
if
((s[i]
&
240
)
==
224
) {
/*
1110xxxx & 1111xxxx == 1110xxxx
*/
c
=
( ( (unsigned
int
) s[i]
&
15
)
<<
12
)
+
( ( (unsigned
int
) s[i
+
1
]
&
63
)
<<
6
)
+
( (unsigned
int
) s[i
+
2
]
&
63
);
i
+=
3
;
}
else
if
((s[i]
&
248
)
==
240
) {
/*
11110xxx & 11111xxx == 11110xxx
*/
c
=
( ( (unsigned
int
) s[i]
&
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[i]
&
252
)
==
248
) {
/*
111110xx & 111111xx == 111110xx
*/
c
=
( ( (unsigned
int
) s[i]
&
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[i]
&
254
)
==
252
) {
/*
1111110x & 1111111x == 1111110x
*/
c
=
( ( (unsigned
int
) s[i]
&
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[i];
if
(isalnum(ch))
{
dst
+=
ch;
}
else
if
(src[i]
==
' ')
{
dst
+=
'
+
';
}
else
{
unsigned
char
c
=
static_cast
<
unsigned
char
>
(src[i]);
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[i]
==
'
%
')
{
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[i]
==
'
+
')
{
dst
+=
' ';
}
else
{
dst
+=
src[i];
}
}
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
;
}
posted on 2007-12-18 16:30 王骏的BLOG 阅读(1006)
评论(3)
编辑
收藏
Comments
#
re: URL编解码(URLEncode,URLDecode)
杨
Posted @ 2008-08-31 17:35
非常好!!!!
#
re: URL编解码(URLEncode,URLDecode)
数据恢复
Posted @ 2008-09-03 10:33
哈,太好了,正找真呢,谢谢。
#
re: URL编解码(URLEncode,URLDecode)
KEN
Posted @ 2008-11-19 16:12
多谢多谢!!!!
标题
姓名
主页
验证码
*
内容
Remember Me?
登录
使用高级评论
Top
[使用Ctrl+Enter键可以直接提交]