VBScript的中文编码解码,与JS的Escape()相同功能

Function vbsEscape(str) as String
        Dim i As Integer
        Dim c As String
        Dim s As String
        Dim a As Integer
        s=""
    For i=1 to Len(str)
        c=Mid(str,i,1)
        a=ASCW(c)
        If (a>=48 and a<=57) or (a>=65 and a<=90) or (a>=97 and a<=122) Then
            s = s & c
        ElseIf InStr("@*_+-./",c)>0 Then
            s = s & c
        ElseIf a>0 and a<16 Then
            s = s & "%0" & Hex(a)
        ElseIf a>=16 and a<256 Then
            s = s & "%" & Hex(a)
        Else
            s = s & "%u" & Hex(a)
        End If
    Next
    return  s
End Function


Function vbsUnEscape(str)
    dim i,s,c
    s=""
    For i=1 to Len(str)
        c=Mid(str,i,1)
        If Mid(str,i,2)="%u" and i<=Len(str)-5 Then
            If IsNumeric("&H" & Mid(str,i+2,4)) Then
                s = s & CHRW(CInt("&H" & Mid(str,i+2,4)))
                i = i+5
            Else
                s = s & c
            End If
        ElseIf c="%" and i<=Len(str)-2 Then
            If IsNumeric("&H" & Mid(str,i+1,2)) Then
                s = s & CHRW(CInt("&H" & Mid(str,i+1,2)))
                i = i+2
            Else
                s = s & c
            End If
        Else
            s = s & c
        End If
    Next
    vbsUnEscape = s
End Function

Function AscIIString(str as string) as String

        Dim j As Integer
        Dim i As Integer
        Dim charArray() As Char
        Dim charNew() As Char
        Dim strNew As String

        charArray = str.ToCharArray()

        For i = 0 To charArray.Length - 1 Step 1
            strNew = strNew & Asc(charArray(i)).ToString("000")
        Next

        charNew = strNew.ToCharArray()
        strNew = ""

        For j = 0 To charNew.Length - 1 Step 2
            If j = charNew.Length - 1 Then
                strNew = strNew & charNew(j).ToString()
            Else
                strNew = strNew & charNew(j + 1).ToString()
                strNew = strNew & charNew(j).ToString()
            End If

        Next

    return strNew

End Function

你可能感兴趣的:(代码片断)