URLEncode、URLDecode-ASP UTF-8 版本

作者: 來源: 日期:2009-10-20

(為了兼容客戶端使用 encodeURIComponent 和 decodeURIComponent,也可在服務器端調用 JS 的這兩個方法,具體請參見在服務器端巧用 JScript 實現 UTF-8 編碼的 URLEncode。千一網絡編輯注)

源代碼收集自網上,已修正其中的錯誤。三個函數:

  • UrlEncode_GBToUtf8 不支持生僻字,比如:鎔。
  • UrlEncode_GBToUtf8_V2 支持生僻字
  • UrlDecode_GBToUtf8 支持生僻字
<%
Function UrlEncode_GBToUtf8(ByVal str)
    Dim B                    ''單個字符
    Dim ub                  ''中文字的Unicode碼(2字節)
    Dim High8b, Low8b       ''Unicode碼的高低位字節
    Dim UtfB1, UtfB2, UtfB3 ''Utf-8碼的三個字節
    Dim i, s
    For i = 1 To Len(str)
        B=Mid(str, i, 1)
        ub = AscW(B)
        If (ub>=48 And ub<=57) Or (ub>=65 And ub<=90) Or (ub>=97 And ub<=122) Or ub=42 Or ub=45 Or ub=46 Or ub=64 Or ub=95 Then
            ''48 to 57代表0~9;65 to 90代表A~Z;97 to 122代表a~z
            ''42代表*;46代表.;64代表@;45代表-;95代表_
            s=s & B
        ElseIf ub=32 Then ''空格轉成+
            s=s & "+"
        ElseIf ub<128 Then    ''低于128的Ascii轉成1個字節
            s=s & "%" & Right("00" & Hex(ub),2)
        Else
            High8b = (ub And &HFF00) / &H100 ''Unicode碼高位
            Low8b = ub And &HFF ''Unicode碼低位
            UtfB1 = (High8b And &HF0) / &H10 Or &HE0 ''取Unicode高位字節的二進制的前4位 + 11100000
            UtfB2 = ((High8b And &HF) * &H4 + (Low8b And &HC0) / &H40) Or &H80 ''取Unicode高位字節的后4位及低位字節的前2位 +10000000
            UtfB3 = (Low8b And &H3F) Or &H80 ''取Unicode低位字節的二進制后6位 + 10000000
            s = s & "%" & Hex(UtfB1) & "%" & Hex(UtfB2) & "%" & Hex(UtfB3)
        End If
    Next
    UrlEncode_GBToUtf8 = s
End Function
 
 
''“漢”-AscW("漢")=27721(十進制)    01101100 01001001(二進制)     6C49(十六進制)
''將Gb2312碼轉成Utf-8碼(十六進制表示)的方法為,先用AscW將Gb2312轉為Unicode碼(2字節),再''將Unicode碼的二進制中的位按utf-8(3字節)模板規則填充 x 位:
 
''URL解碼,Gb2312頁面提交到Utf-8頁面
Function UrlDecode_GBToUtf8(ByVal str)
    Dim B,ub    ''中文字的Unicode碼(2字節)
    Dim UtfB    ''Utf-8單個字節
    Dim UtfB1, UtfB2, UtfB3 ''Utf-8碼的三個字節
    Dim i, n, s
    n=0
    ub=0
    For i = 1 To Len(str)
        B=Mid(str, i, 1)
        Select Case B
            Case "+"
                s=s & " "
            Case "%"
                ub=Mid(str, i + 1, 2)
                UtfB = CInt("&H" & ub)
                If UtfB<128 Then
                    i=i+2
                    s=s & ChrW(UtfB)
                Else
                    UtfB1=(UtfB And &H0F) * &H1000    ''取第1個Utf-8字節的二進制后4位
                    UtfB2=(CInt("&H" & Mid(str, i + 4, 2)) And &H3F) * &H40        ''取第2個Utf-8字節的二進制后6位
                    UtfB3=CInt("&H" & Mid(str, i + 7, 2)) And &H3F        ''取第3個Utf-8字節的二進制后6位
                    s=s & ChrW(UtfB1 Or UtfB2 Or UtfB3)
                    i=i+8
                End If
            Case Else    ''Ascii碼
                s=s & B
        End Select
    Next
    UrlDecode_GBToUtf8 = s
End Function
 
 
Private Function UrlEncode_GBToUtf8_V2(szInput)
    Dim wch, uch, szRet
    Dim x
    Dim nAsc, nAsc2, nAsc3
    If szInput = "" Then
        UrlEncode_GBToUtf8_V2= szInput
        Exit Function
    End If
    For x = 1 To Len(szInput)
        wch = Mid(szInput, x, 1)
        nAsc = AscW(wch)
        If nAsc < 0 Then nAsc = nAsc + 65536
        If wch = "+" then
            szRet = szRet & "%2B"
        ElseIf wch = "%" then
            szRet = szRet & "%25"
        ElseIf (nAsc And &HFF80) = 0 Then
            szRet = szRet & wch
        Else
            If (nAsc And &HF000) = 0 Then
                uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
                szRet = szRet & uch
            Else
                uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
                Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
                Hex(nAsc And &H3F Or &H80)
                szRet = szRet & uch
            End If
        End If
    Next
    UrlEncode_GBToUtf8_V2= szRet
End Function
 
 
dim str
str = "1?a千一"
Response.Write(UrlEncode_GBToUtf8(str) & "<br />")
Response.Write(UrlDecode_GBToUtf8(UrlEncode_GBToUtf8(str)) & "<br />")
%>

結果為

1%3Fa%E5%8D%83%E4%B8%80
1?a千一

你前面那位網友看了:C# 方法、函數的值如何傳遞

▲▲▲嘿,歡迎轉載傳播本站原創文章,盡量保留來源噢。▲▲▲

文章評論
標題:必填
內容:
最新22选5开奖公告