ASP编码和解码函数详解

用ASP开发的时候遇到一个解码问题。虽然在ASP中使用Request获取编码过URL字符串会自动解码,但是Request.BinaryRead(Request.TotalBytes)取得Post数据时却不会解码,所以只能手动进行解码。
ASP解码函数:

Function URLDecode(enStr) 
 dim deStr,strSpecial 
 dim c,i,v 
  deStr=""
  strSpecial="!""#$%&'()*+,.-_/:;<=>?@[/]^`{|}~%"
  for i=1 to len(enStr) 
   c=Mid(enStr,i,1) 
   if c="%" then 
    v=eval("&h"+Mid(enStr,i+1,2)) 
    if inStr(strSpecial,chr(v))>0 then 
     deStr=deStr&chr(v) 
     i=i+2 
    else
     v=eval("&h"+ Mid(enStr,i+1,2) + Mid(enStr,i+4,2)) 
     deStr=deStr & chr(v) 
     i=i+5 
    end if
   else
    if c="+" then 
     deStr=deStr&" "
    else
     deStr=deStr&c 
    end if
   end if
  next 
  URLDecode=deStr 
End function

只是个人爱好,自己研究了一下编码的实现思路,最后自己写了一个编码函数,提供大家参考。注:ASP有内置的编码函数,即是Server.URLEncode。

ASP编码函数:

private Function URLEncoding(vstrIn) 
  strReturn = ""
  For i = 1 To Len(vstrIn) 
  ThisChr = Mid(vStrIn,i,1) 
  If Abs(Asc(ThisChr)) < &HFF Then 
  strReturn = strReturn & ThisChr 
  Else 
  innerCode = Asc(ThisChr) 
  If innerCode < 0 Then 
  innerCode = innerCode + &H10000 
  End If 
  Hight8 = (innerCode And &HFF00)/ &HFF 
  Low8 = innerCode And &HFF 
  strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8) 
  End If 
  Next 
  URLEncoding = strReturn 
End Function

建议大家在中文编码的时候,还是使用ASP 内置的函数。虽然上面这个编码函数测试过N 遍了,没有发现问题,但是以防万一存在Bug。

以上就是关于ASP编码和解码函数,希望对大家的学习有所帮助。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2327.html