메뉴 건너뛰기

Dev tips

ASP URLDecode, URLEncode Ansi, UTF-8 모음

taknim 2011.04.02 21:20 조회 수 : 8570 추천:1714

http://blog.naver.com/falconer00/80010885656Function toUTF8(szSource)
        Dim szChar
        Dim WideChar
        Dim nLength
        Dim i

        toUTF8 = ""
        nLength = Len(szSource)
        For i = 1 To nLength
                szChar = Mid(szSource, i, 1)

                If Asc(szChar) < 0 Then
                        WideChar = CLng(AscB(MidB(szChar, 2, 1))) * 256 + AscB(MidB(szChar, 1, 1))

                        If (WideChar And &HFF80) = 0 Then
                                toUTF8 = toUTF8 & "%" & Hex(WideChar)
                        ElseIf (WideChar And &HF000) = 0 Then
                                toUTF8 = toUTF8 & _
                                          "%" & Hex(CInt((WideChar And &HFFC0) / 64) Or &HC0) & _
                                          "%" & Hex(WideChar And &H3F Or &H80)
                        Else
                                toUTF8 = toUTF8 & _
                                          "%" & Hex(CInt((WideChar And &HF000) / 4096) Or &HE0) & _
                                          "%" & Hex(CInt((WideChar And &HFFC0) / 64) And &H3F Or &H80) & _
                                          "%" & Hex(WideChar And &H3F Or &H80)
                        End If
                Else
                        toUTF8 = toUTF8 + szChar
                End If
        Next
        Exit Function
End Function

Function URLDecode(Expression)
        Dim strSource, strTemp, strResult, strchr
        Dim lngPos, AddNum, IFKor        
        strSource = Replace(Expression, "+", " ")
        For lngPos = 1 To Len(strSource)
                AddNum        = 2
                strTemp = Mid(strSource, lngPos, 1)
                If strTemp = "%" Then
                        If lngPos + AddNum < Len(strSource) + 1 Then
                                strchr = CInt("&H" & Mid(strSource, lngPos + 1, AddNum))
                                If strchr > 130 Then
                                        AddNum = 5
                                        IFKor = Mid(strSource, lngPos + 1, AddNum)
                                        IFKor = Replace(IFKor, "%", "")
                                        strchr = CInt("&H" & IFKor )
                                End If
                                strResult = strResult & Chr(strchr)
                                lngPos        = lngPos + AddNum
                        End If
                Else
                        strResult = strResult & strTemp
                End If
        Next        
        URLDecode = strResult
End Function

Function UrlDecode_GBToUtf8(ByVal str)
        Dim B,ub
        Dim UtfB
        Dim UtfB1, UtfB2, UtfB3
        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
                                        UtfB2=(CInt("&H" & Mid(str, i + 4, 2)) And &H3F) * &H40
                                        UtfB3=CInt("&H" & Mid(str, i + 7, 2)) And &H3F
                                        s=s & ChrW(UtfB1 Or UtfB2 Or UtfB3)
                                        i=i+8
                                End If
                        Case Else
                                s=s & B
                End Select
        Next
        UrlDecode_GBToUtf8 = s
End Function

--------------------------------------------------------------------------------
runat="server" 으로 함수 구현
<script language="javascript" runat="server">
        function URLDecode(v) {
                return decodeURI(v);
        }
        function URLEncode(v) {
                return encodeURI(v);
        }
</script>