Url编码解码函数合集 含utf-8和gb2312

来源:互联网 发布:nicelabel 数据库密码 编辑:程序博客网 时间:2024/06/05 19:41

转载:http://www.yuzhe.name/Item.asp?id=109

在浏览器中打开一个网址,如果网址中含中文字符时浏览器会自动根据当前页面的编码进行对就的编码。我们大家最常见到的就是用google和百度进行搜索时,中文关键字就是以编码后的形式显示在浏览器的地址栏里面。

最近在做一个程序时,要用到获取搜索引擎的关键字功能。在网上找了很多关键这样的函数代码,然后均不是通用的,要么只能在gb2312编码的页面中得到对应的url为gb2312编码的结果,要么就是只能在utf-8的页面中得到url编码为utf-8的解码。但目前主流搜索引擎Google和百度的编码却不一样。

Google采用了utf-8国际编码,而百度却用的是gb2312编码。比如我们在google和百度搜索“雨哲”,编码后的url路径为:

  • Google:http://www.google.cn/search?q=%E9%9B%A8%E5%93%B2
  • 百度:http://www.baidu.com/s?wd=%D3%EA%D5%DC

也就是说:

  • “雨哲”的UTF-8编码为:%E9%9B%A8%E5%93%B2
  • “雨哲”的GB2312编码为:%D3%EA%D5%DC

网上有很多url编码解码的函数,但均只能在单一页面对对应的编码进行解码,我这篇文章说的重点就在这里:如何在一种编码的页面中能通时对中文进行两种编码的编码,而且也能同时对两种编码进行对就的解码。(注:以下所说的代码如未作特别说明,均指在utf-8页面环境中使用)

一、Url编码通用代码:

  1. '==================================================
  2. '函 数 名:   YuZhe_UrlEncode
  3. '功    能:   将指定字符进行指定格式进行编码
  4. '参    数:    iStrCode 目标字符
  5. '             iPageCode 指定编码 65001-UTF-8或936-GB2312
  6. '==================================================
  7. Function YuZhe_UrlEncode(iStrCode, iPageCode)
  8.     Dim StrUrlEncode
  9.     StrUrlEncode = "" '初始化变量
  10.     If iPageCode <> SetPageCode Then '如果指定要编码的类型与当前页面编码不一至,则临时设置处理该函数时的页面编码类型
  11.         Session.CodePage = iPageCode
  12.         StrUrlEncode = Server.UrlEncode(iStrCode)
  13.         Session.CodePage = SetPageCode '还原页面编码为默认
  14.     Else
  15.         StrUrlEncode = Server.UrlEncode(iStrCode)
  16.     End If
  17.     YuZhe_UrlEncode = StrUrlEncode
  18. End Function
  19. 复制保存

示例:

  1. <%
  2. Const SetPageCode = "UTF-8" '定义当前文件编码类型
  3. Response.Write "输出UTF-8编码" & YuZhe_UrlEncode("雨哲", 65001)
  4. Response.Write "输出GB2312编码" & YuZhe_UrlEncode("雨哲", 936)
  5. %>
  6. 复制保存

二、URl解码函数:

1.UTF-8解码函数:

  1. '==================================================
  2. '函数名:    UTF2GB
  3. '功能:      将utf-8编码解码为中文
  4. 'UTFStr:     需要编码的字符,在utf-8和gb2312两种页面编码中均可使用
  5. '==================================================
  6. function UTF2GB(UTFStr)
  7. Dim dig,GBSTR
  8.     for Dig=1 to len(UTFStr)
  9.         if mid(UTFStr,Dig,1)="%" then
  10.             if len(UTFStr) >= Dig+8 then
  11.                 GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
  12.                 Dig=Dig+8
  13.             else
  14.                 GBStr=GBStr & mid(UTFStr,Dig,1)
  15.             end if
  16.         else
  17.             GBStr=GBStr & mid(UTFStr,Dig,1)
  18.         end if
  19.     next
  20.     UTF2GB=GBStr
  21. end function 
  22. function ConvChinese(x) 
  23. dim a,i,j,DigS,Unicode
  24.     A=split(mid(x,2),"%")
  25.     i=0
  26.     j=0
  27.     
  28.     for i=0 to ubound(A) 
  29.         A(i)=c16to2(A(i))
  30.     next
  31.         
  32.     for i=0 to ubound(A)-1
  33.         DigS=instr(A(i),"0")
  34.         Unicode=""
  35.         for j=1 to DigS-1
  36.             if j=1 then 
  37.                 A(i)=right(A(i),len(A(i))-DigS)
  38.                 Unicode=Unicode & A(i)
  39.             else
  40.                 i=i+1
  41.                 A(i)=right(A(i),len(A(i))-2)
  42.                 Unicode=Unicode & A(i) 
  43.             end if 
  44.         next
  45.         
  46.         if len(c2to16(Unicode))=4 then
  47.             ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
  48.         else
  49.             ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
  50.         end if
  51.     next
  52. end function 
  53. function c16to2(x)
  54. '这个函数是用来转换16进制到2进制的,可以是任何长度的,一般转换UTF-8的时候是两个长度,比如A9
  55. '比如:输入“C2”,转化成“11000010”,其中1100是"c"是10进制的12(1100),那么2(10)不足4位要补齐成(0010)。
  56. dim tempstr
  57. dim i:i=0'临时的指针 
  58. for i=1 to len(trim(x))
  59.   tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
  60.   do while len(tempstr)<4
  61.    tempstr="0" & tempstr'如果不足4位那么补齐4位数
  62.   loop
  63.   c16to2=c16to2 & tempstr
  64. next
  65. end function 
  66. function c2to16(x)
  67.   '2进制到16进制的转换,每4个0或1转换成一个16进制字母,输入长度当然不可能不是4的倍数了 
  68.   dim i:i=1'临时的指针
  69.   for i=1 to len(x)  step 4
  70.    c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
  71.   next
  72. end function 
  73. function c2to10(x)
  74.   '单纯的2进制到10进制的转换,不考虑转16进制所需要的4位前零补齐。
  75.   '因为这个函数很有用!以后也会用到,做过通讯和硬件的人应该知道。
  76.   '这里用字符串代表二进制
  77.    c2to10=0
  78.    if x="0" then exit function'如果是0的话直接得0就完事
  79.    dim i:i=0'临时的指针
  80.    for i= 0 to len(x) -1'否则利用8421码计算,这个从我最开始学计算机的时候就会,好怀念当初教我们的谢道建老先生啊!
  81.     if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
  82.    next
  83. end function 
  84. function c10to2(x)
  85. '10进制到2进制的转换
  86.   dim sign, result
  87.   result = ""
  88.   '符号
  89.   sign = sgn(x)
  90.   x = abs(x)
  91.   if x = 0 then
  92.     c10to2 = 0
  93.     exit function
  94.   end if
  95.   do until x = "0"
  96.     result = result & (x mod 2)
  97.     x = x \ 2
  98.   loop
  99.   result = strReverse(result)
  100.   if sign = -1 then
  101.     c10to2 = "-" & result
  102.   else
  103.     c10to2 = result
  104.   end if
  105. end function
  106. '雨哲注:本函数代码来自网络
  107. 复制保存

示例:

  1. <%
  2. Response.Write "解码%E9%9B%A8%E5%93%B2" & UTF2GB("%E9%9B%A8%E5%93%B2")
  3. %>
  4. 复制保存

2.GB2312解码函数:

  1. '==================================================
  2. '函数名:    Gb2312UrlDecode
  3. '功能:      将Gb2312Url编码解码为中文
  4. 'EncodeStr:  需要解码的GB2312编码,仅在GB2312页面编码中有效
  5. '==================================================
  6. Function Gb2312UrlDecode(DecodeStr)
  7.     'On Error Resume Next
  8.     If DecodeStr = "" Or IsNull(DecodeStr) Then Exit Function
  9.     Dim NewGb2312UrlDecodeStr,HaveGb2312UrlDecodeChar,LastGb2312UrlDecodeChar
  10.     Dim iGb2312UrlDecode,Gb2312UrlDecodeChar_C,Gb2312UrlDecodeChar_C_1,Gb2312UrlDecodeChar_Num_1
  11.     NewGb2312UrlDecodeStr = "" 
  12.     HaveGb2312UrlDecodeChar = False 
  13.     LastGb2312UrlDecodeChar = "" 
  14.     For iGb2312UrlDecode = 1 To Len(DecodeStr)
  15.         Gb2312UrlDecodeChar_C = mid(DecodeStr,iGb2312UrlDecode,1) 
  16.         If Gb2312UrlDecodeChar_C = "+" Then 
  17.             NewGb2312UrlDecodeStr = NewGb2312UrlDecodeStr & " " 
  18.         ElseIf Gb2312UrlDecodeChar_C = "%" Then 
  19.             Gb2312UrlDecodeChar_C_1 = Mid(DecodeStr,iGb2312UrlDecode+1,2) 
  20.             Gb2312UrlDecodeChar_Num_1 = Cint("&H" & Gb2312UrlDecodeChar_C_1) 
  21.             If HaveGb2312UrlDecodeChar Then 
  22.                 HaveGb2312UrlDecodeChar = False 
  23.                 NewGb2312UrlDecodeStr = NewGb2312UrlDecodeStr & Chr(Cint("&H" & LastGb2312UrlDecodeChar & Gb2312UrlDecodeChar_C_1)) 
  24.             Else  
  25.                 If abs(Gb2312UrlDecodeChar_Num_1) <= 127 then 
  26.                     NewGb2312UrlDecodeStr = NewGb231 
  27.                     HaveGb2312UrlDecodeChar = True 
  28.                     LastGb2312UrlDecodeChar = Gb2312UrlDecodeChar_C_1 
  29.                 End If 
  30.             End if 
  31.             iGb2312UrlDecode = iGb2312UrlDecode+2 
  32.         Else 
  33.             NewGb2312UrlDecodeStr = NewGb2312UrlDecodeStr & Gb2312UrlDecodeChar_C
  34.         End If
  35.     Next
  36.     Gb2312UrlDecode = NewGb2312UrlDecodeStr
  37. End Function
  38. '雨哲注:该函数代码由雨哲修改于网络
  39. 复制保存

示例:

  1. <%
  2. Const SetPageCode = "65001" '定义当前文件编码类型 
  3. If SetPageCode <> 936 Then
  4.     Session.CodePage = 936
  5.     Response.Write "%D3%EA%D5%DC解码结果" & Gb2312UrlDecode("%D3%EA%D5%DC")
  6.     Session.CodePage = SetPageCode
  7. Else
  8.     Response.Write "%D3%EA%D5%DC解码结果" & Gb2312UrlDecode("%D3%EA%D5%DC")
  9. End If
  10. %>
  11. 复制保存

版权申明:转载请注明来源“雨哲在线”。本信息与以上内容是不可分割的。


0 0
原创粉丝点击