메뉴 건너뛰기

Dev tips

ASP Ansi(euc-kr, euckr)에서 URLEncode, URLDecode 구현

taknim 2011.04.02 21:07 조회 수 : 8602 추천:2071

http://www.cftea.com/c/2009/10/6W0HDM9PUXEBA4NB.aspServer.URLEncode 와 URLEncode 가 동일한 결과로 출력.
URLDecode 를 하면 원래 문자열이 나온다...

<%
Function URLEncode_Gb(ByVal str)
    Dim i,s
    Dim B,bCode,gb,Hight8b,Low8b
    s = ""
    For i = 1 To Len(str)
        B = Mid(str,i,1)
        bCode=Abs(Asc(B))
        If (bCode>=48 And bCode<=57) Or (bCode>=65 And bCode<=90) Or (bCode>=97 And bCode<=122) Or bCode=42 Or bCode=45 Or bCode=46 Or bCode=64 Or bCode=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 bCode=32 Then ''空格?成+
            s=s & "+"
        ElseIf bCode<128 Then    ''低于128的Ascii?成1?字?
            s=s & "%" & Right("00" & Hex(bCode),2)
        Else
            gb = Asc(B)
            If gb < 0 Then
                gb = gb + &H10000    ''gb?????,要加上65536
            End If
            Hight8b = (gb  And &HFF00) / &H100    ''二?制高8位
            Low8b = gb And &HFF    ''二?制低8位
            s = s & "%" & Hex(Hight8b) &  "%" & Hex(Low8b)
        End If  
    Next
    URLEncode_Gb = s
End Function


Function URLDecode_Gb(ByVal str)
    Dim i,s
    Dim B,bCode,gb,Hight8b,Low8b
    s = ""
    For i = 1 To Len(str)
        B = Mid(str,i,1)
        Select Case B
            Case "+"
                s=s & " "
            Case "%"
                gb=Mid(str,i+1,2)
                bCode=CInt("&H" & gb)
                If bCode<128 Then
                    i=i+2
                Else
                    bCode=CInt("&H" & gb & Mid(str,i+4,2))
                    i=i+5
                End If
                s=s & Chr(bCode)
            Case Else
                s=s & B
        End Select
    Next
    URLDecode_Gb = s
End Function


dim str
str = "1?a千一"
Response.Write(Server.URLEncode(str) & "<br />")
Response.Write(URLEncode_Gb(str) & "<br />")
Response.Write(URLDecode_Gb(str) & "<br />")
%>

결과

1%3Fa%C7%A7%D2%BB
1%3Fa%C7%A7%D2%BB
1?a千一