ASP編碼和解碼函數詳解_應用技巧

來源:互聯網
上載者:User

用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編碼和解碼函數,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.