ASP編碼和解碼函數(shù)詳解
用ASP開發(fā)的時候遇到一個解碼問題。雖然在ASP中使用Request獲取編碼過URL字符串會自動解碼,但是Request.BinaryRead(Request.TotalBytes)取得Post數(shù)據(jù)時卻不會解碼,所以只能手動進行解碼。
ASP解碼函數(shù):
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
只是個人愛好,自己研究了一下編碼的實現(xiàn)思路,最后自己寫了一個編碼函數(shù),提供大家參考。注:ASP有內(nèi)置的編碼函數(shù),即是Server.URLEncode。
ASP編碼函數(shù):
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èi)置的函數(shù)。雖然上面這個編碼函數(shù)測試過N 遍了,沒有發(fā)現(xiàn)問題,但是以防萬一存在Bug。
以上就是關(guān)于ASP編碼和解碼函數(shù),希望對大家的學(xué)習有所幫助。
相關(guān)文章
asp下用replace非正則實現(xiàn)代碼運行功能的代碼
asp下用replace非正則實現(xiàn)代碼運行功能的代碼...2007-09-09
解決 JScript 中使用日期類型數(shù)據(jù)時出現(xiàn)類型錯誤的問題
在服務(wù)端JScript中,有些時候需要使用日期類型的數(shù)據(jù),比如說設(shè)置一個cookie的到期時間,更新數(shù)據(jù)庫中的日期字段等。但是按照平常的方法來用js里的date類型數(shù)據(jù)時,運行時就會發(fā)生錯誤。2008-05-05
asp實現(xiàn)批量插入表單中的數(shù)據(jù)到數(shù)據(jù)庫的方法
asp實現(xiàn)批量插入表單中的數(shù)據(jù)到數(shù)據(jù)庫的方法...2007-08-08
asp下利用fso實現(xiàn)文件夾或文件移動改名等操作函數(shù)
以后利用fso來操作文件和文件夾就方便了2008-01-01
asp下實現(xiàn)對HTML代碼進行轉(zhuǎn)換的函數(shù)
asp下實現(xiàn)對HTML代碼進行轉(zhuǎn)換的函數(shù)...2007-08-08

