💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
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编码通用代码: ~~~ '================================================== '函 数 名: YuZhe_UrlEncode '功 能: 将指定字符进行指定格式进行编码 '参 数: iStrCode 目标字符 ' iPageCode 指定编码 65001-UTF-8或936-GB2312 '================================================== Function YuZhe_UrlEncode(iStrCode, iPageCode) Dim StrUrlEncode StrUrlEncode = "" '初始化变量 If iPageCode <> SetPageCode Then '如果指定要编码的类型与当前页面编码不一至,则临时设置处理该函数时的页面编码类型 Session.CodePage = iPageCode StrUrlEncode = Server.UrlEncode(iStrCode) Session.CodePage = SetPageCode '还原页面编码为默认 Else StrUrlEncode = Server.UrlEncode(iStrCode) End If YuZhe_UrlEncode = StrUrlEncode End Function ~~~ 示例: ~~~ <% Const SetPageCode = "UTF-8" '定义当前文件编码类型 Response.Write "输出UTF-8编码" & YuZhe_UrlEncode("雨哲", 65001) Response.Write "输出GB2312编码" & YuZhe_UrlEncode("雨哲", 936) %> ~~~ 二、URl解码函数: 1.UTF-8解码函数: ~~~ '================================================== '函数名: UTF2GB '功能: 将utf-8编码解码为中文 'UTFStr: 需要编码的字符,在utf-8和gb2312两种页面编码中均可使用 '================================================== function UTF2GB(UTFStr) Dim dig,GBSTR for Dig=1 to len(UTFStr) if mid(UTFStr,Dig,1)="%" then if len(UTFStr) >= Dig+8 then GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9)) Dig=Dig+8 else GBStr=GBStr & mid(UTFStr,Dig,1) end if else GBStr=GBStr & mid(UTFStr,Dig,1) end if next UTF2GB=GBStr end function function ConvChinese(x) dim a,i,j,DigS,Unicode A=split(mid(x,2),"%") i=0 j=0 for i=0 to ubound(A) A(i)=c16to2(A(i)) next for i=0 to ubound(A)-1 DigS=instr(A(i),"0") Unicode="" for j=1 to DigS-1 if j=1 then A(i)=right(A(i),len(A(i))-DigS) Unicode=Unicode & A(i) else i=i+1 A(i)=right(A(i),len(A(i))-2) Unicode=Unicode & A(i) end if next if len(c2to16(Unicode))=4 then ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode))) else ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode))) end if next end function function c16to2(x) '这个函数是用来转换16进制到2进制的,可以是任何长度的,一般转换UTF-8的时候是两个长度,比如A9 '比如:输入“C2”,转化成“11000010”,其中1100是"c"是10进制的12(1100),那么2(10)不足4位要补齐成(0010)。 dim tempstr dim i:i=0'临时的指针 for i=1 to len(trim(x)) tempstr= c10to2(cint(int("&h" & mid(x,i,1)))) do while len(tempstr)<4 tempstr="0" & tempstr'如果不足4位那么补齐4位数 loop c16to2=c16to2 & tempstr next end function function c2to16(x) '2进制到16进制的转换,每4个0或1转换成一个16进制字母,输入长度当然不可能不是4的倍数了 dim i:i=1'临时的指针 for i=1 to len(x) step 4 c2to16=c2to16 & hex(c2to10(mid(x,i,4))) next end function function c2to10(x) '单纯的2进制到10进制的转换,不考虑转16进制所需要的4位前零补齐。 '因为这个函数很有用!以后也会用到,做过通讯和硬件的人应该知道。 '这里用字符串代表二进制 c2to10=0 if x="0" then exit function'如果是0的话直接得0就完事 dim i:i=0'临时的指针 for i= 0 to len(x) -1'否则利用8421码计算,这个从我最开始学计算机的时候就会,好怀念当初教我们的谢道建老先生啊! if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i) next end function function c10to2(x) '10进制到2进制的转换 dim sign, result result = "" '符号 sign = sgn(x) x = abs(x) if x = 0 then c10to2 = 0 exit function end if do until x = "0" result = result & (x mod 2) x = x \ 2 loop result = strReverse(result) if sign = -1 then c10to2 = "-" & result else c10to2 = result end if end function '雨哲注:本函数代码来自网络 ~~~ 示例: ~~~ <% Response.Write "解码%E9%9B%A8%E5%93%B2" & UTF2GB("%E9%9B%A8%E5%93%B2") %> ~~~ 2.GB2312解码函数: ~~~ '================================================== '函数名: Gb2312UrlDecode '功能: 将Gb2312Url编码解码为中文 'EncodeStr: 需要解码的GB2312编码,仅在GB2312页面编码中有效 '================================================== Function Gb2312UrlDecode(DecodeStr) 'On Error Resume Next If DecodeStr = "" Or IsNull(DecodeStr) Then Exit Function Dim NewGb2312UrlDecodeStr,HaveGb2312UrlDecodeChar,LastGb2312UrlDecodeChar Dim iGb2312UrlDecode,Gb2312UrlDecodeChar_C,Gb2312UrlDecodeChar_C_1,Gb2312UrlDecodeChar_Num_1 NewGb2312UrlDecodeStr = "" HaveGb2312UrlDecodeChar = False LastGb2312UrlDecodeChar = "" For iGb2312UrlDecode = 1 To Len(DecodeStr) Gb2312UrlDecodeChar_C = mid(DecodeStr,iGb2312UrlDecode,1) If Gb2312UrlDecodeChar_C = "+" Then NewGb2312UrlDecodeStr = NewGb2312UrlDecodeStr & " " ElseIf Gb2312UrlDecodeChar_C = "%" Then Gb2312UrlDecodeChar_C_1 = Mid(DecodeStr,iGb2312UrlDecode+1,2) Gb2312UrlDecodeChar_Num_1 = Cint("&H" & Gb2312UrlDecodeChar_C_1) If HaveGb2312UrlDecodeChar Then HaveGb2312UrlDecodeChar = False NewGb2312UrlDecodeStr = NewGb2312UrlDecodeStr & Chr(Cint("&H" & LastGb2312UrlDecodeChar & Gb2312UrlDecodeChar_C_1)) Else If abs(Gb2312UrlDecodeChar_Num_1) <= 127 then NewGb2312UrlDecodeStr = NewGb231 HaveGb2312UrlDecodeChar = True LastGb2312UrlDecodeChar = Gb2312UrlDecodeChar_C_1 End If End if iGb2312UrlDecode = iGb2312UrlDecode+2 Else NewGb2312UrlDecodeStr = NewGb2312UrlDecodeStr & Gb2312UrlDecodeChar_C End If Next Gb2312UrlDecode = NewGb2312UrlDecodeStr End Function ~~~ 示例: ~~~ <% Const SetPageCode = "65001" '定义当前文件编码类型 If SetPageCode <> 936 Then Session.CodePage = 936 Response.Write "%D3%EA%D5%DC解码结果" & Gb2312UrlDecode("%D3%EA%D5%DC") Session.CodePage = SetPageCode Else Response.Write "%D3%EA%D5%DC解码结果" & Gb2312UrlDecode("%D3%EA%D5%DC") End If %> ~~~ 版权申明:转载请注明来源“雨哲在线”。本信息与以上内容是不可分割的。