请问大家~~
ASP里头有没有一个类似JAVA中的URLDecoder的函数呢?~~
如下:
java.net.URLDecoder.decode("%E6%94%AF%E4%B8%9D%E5%85%89%E6%AF%9B%E5%B7%BE","UTF-8");
也就是说把一串用UTF-8的URL字串还原成原来的样子~~~
即:
比如说我在GOOGLE里搜索“中国”,得到:
http://www.google.com/search?q=%E4%B8%AD%E5%9B%BD&ie=UTF-8&oe=UTF-8&hl=zh-CN&lr=
的URL,那么怎么样才能把它还原成:
http://www.google.com/search?q中国&ie=UTF-8&oe=UTF-8&hl=zh-CN&lr=
呢?~~
有么有呢~~~?~~
---------------------------------------------------------------
server.htmlEncode()
---------------------------------------------------------------
Server.URLEncode(String)
把一字符串转换成URL编码规则格式。String是要转换的字符串
---------------------------------------------------------------
还原函数:
1<script language="vbscript">
2dim str
3str="%C3%B7%BB%A8%D1%A9"
4alert URLDecode(str)
5function URLDecode(enStr)
6dim deStr,strSpecial
7dim c,i,v
8deStr=""
9strSpecial="!""#$%&'()*+,/:;<=>?@[\\]^`{ ¦}~%"
10for i=1 to len(enStr)
11c=Mid(enStr,i,1)
12if c="%" then
13v=eval("&h"+Mid(enStr,i+1,2))
14if inStr(strSpecial,chr(v))>0 then
15deStr=deStr&chr(v)
16i=i+2
17else
18v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
19deStr=deStr&chr(v)
20i=i+5
21end if
22else
23if c="+" then
24deStr=deStr&" "
25else
26deStr=deStr&c
27end if
28end if
29next
30URLDecode=deStr
31end function
32</script>
---------------------------------------------------------------
上面说的都是反函数,我这个才是你要的!
输入:"http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=%E5%85%B3%E9%94%AE%E5%AD%97&btnG=Google+Search"
输出:关键字
1<html>
2<head>
3<meta content="VBScript" name="VI60_defaultClientScript"/>
4<meta content="Microsoft Visual Studio 6.0" name="GENERATOR"/>
5<title></title>
6<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/></head>
7<body><pre>
8输入:"http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=%E5%85%B3%E9%94%AE%E5%AD%97&btnG=Google+Search"
9输出:关键字</pre>
10<script language="vbscript">
11<!--
12
13mystr="http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=%E5%85%B3%E9%94%AE%E5%AD%97&btnG=Google+Search"
14
15function getutf8(x)
16'这个函数是用来得到%号的部分,
17'输入条件是""http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=%E5%85%B3%E9%94%AE%E5%AD%97&btnG=Google+Search"
18A=split(x,"&")'定义一个临时数组
19dim i:i=0'临时的指针
20for i=0 to ubound(A)
21if instr(A(i),"%")>0 then
22B=split(A(i),"=")'靠!再定义一个临时数组,省事,浪费内存比浪费我的生命强
23getutf8=B(1)
24exit for
25end if
26next
27getutf8=right(getutf8,len(getutf8)-1)'去掉左边的%
28end function
29
30msgbox U8toU(getutf8(mystr))
31
32function c16to2(x)
33'这个函数是用来转换16进制到2进制的,可以是任何长度的,一般转换UTF-8的时候是两个长度,比如A9
34'比如:输入“C2”,转化成“11000010”,其中1100是"c"是10进制的12(1100),那么2(10)不足4位要补齐成(0010)。
35dim tempstr
36dim i:i=0'临时的指针
37for i=1 to len(trim(x))
38tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
39do while len(tempstr)<4
40tempstr="0" & tempstr'如果不足4位那么补齐4位数
41loop
42c16to2=c16to2 & tempstr
43next
44end function
45
46
47
48'document.write hex(asc("字")) & "<br>"
49
50function U8toU(x)
51'输入一堆有%分隔的字符串,先分成数组,根据utf8规则来判断补齐规则
52'输入:关 E5 85 B3 键 E9 94 AE 字 E5 AD 97
53'输出:关 B9D8 键 BCFC 字 D7D6
54dim WeiS'要判断第一个编码的位数
55dim Unicode'二进制的Unicode码
56dim alpha'定义单个字符
57A=split(x,"%")'定义一个临时数组
58dim i:i=0'临时的指针
59dim j:j=0'临时的指针
60
61for i=0 to ubound(A)
62A(i)=c16to2(A(i))'第一次循环,先转换成2进制再说
63
64next
65
66for i=0 to ubound(A)-1
67WeiS=instr(A(i),"0")'判断第一次出现0的位置,
68'可能是1(单字节),3(3-1字节),4,5,6,7不可能是2和大于7
69'理论上到7,实际不会超过3。
70
71Unicode=""
72for j=1 to WeiS-1
73if j=1 then
74A(i)=right(A(i),len(A(i))-WeiS)'第一个去掉最左边的WeiS个
75Unicode=Unicode & A(i)
76
77else
78i=i+1
79A(i)=right(A(i),len(A(i))-2)'其余去掉最左边的两个
80Unicode=Unicode & A(i)
81end if
82
83next
84U8toU=U8toU & chrw(int("&H" & c2to16(Unicode)))'总算完了,妈的!!
85
86
87next
88
89end function
90'msgbox c2to16("11100101")
91
92function c2to16(x)
93'2进制到16进制的转换,每4个0或1转换成一个16进制字母,输入长度当然不可能不是4的倍数了
94
95dim i:i=1'临时的指针
96for i=1 to len(x) step 4
97c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
98next
99end function
100
101function c2to10(x)
102'单纯的2进制到10进制的转换,不考虑转16进制所需要的4位前零补齐。
103'因为这个函数很有用!以后也会用到,做过通讯和硬件的人应该知道。
104'这里用字符串代表二进制
105dim mysign :mysign=sgn(cint(x))'定义mysign这个东西,首先判断正负符号
106x=abs(cint(int(x)))
107c2to10=0
108if x="0" then exit function'如果是0的话直接得0就完事
109dim i:i=0'临时的指针
110for i= 0 to len(x) -1'否则利用8421码计算,这个从我最开始学计算机的时候就会,好怀念当初教我们的谢道建老先生啊!
111if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
112next
113if mysign=-1 then c2to10=-1 * c2to10'加上正负符号
114end function
115
116
117function c10to2(x)'10进制到2进制的转换
118'这个函数在计算16位到2位转换时候用到了,
119'没有做在16位里面是因为这个函数只是单纯10-2转换,不涉及16进制由4个2进制补齐空位,将来可以用到任何地方
120'比如输入2,输出“10”而不是“0010”
121'首先判断正负符号
122dim mysign:mysign=sgn(x)'定义一个符号标记
123x=abs(x)
124'然后判断有几位,至少一位
125dim WeiS:WeiS=1
126do
127if x<2^WeiS then
128exit do
129else
130WeiS=WeiS+1
131end if
132loop
133dim tempnum:tempnum=x'定义一个临时的数字
134dim i:i=0'临时的指针
135for i= WeiS to 1 step-1
136if tempnum>=2^(i-1) then
137tempnum=tempnum-2^(i-1)
138c10to2=c10to2 & "1"
139else
140c10to2=c10to2 & "0"
141end if
142next
143if mysign=-1 then c10to2="-" & c10to2'加上正负符号
144end function
145
146
147\-->
148</script>
149</body>
150</html>