encode characters

I am searching an encoder for VB, that encode characters (like java script escape function).

Does anyone have it?
LVL 1
sivanpAsked:
Who is Participating?
 
autConnect With a Mentor Commented:
Here's the URLEncode function:
'-----------------------
Public Function URLEncode(sDecoded As String) As String
    'This function URL-encodes a string
    '
    Dim Ascii As Byte, Char As String * 1
    Dim i As Integer
    Dim sOutput As String
    Dim TwoHex As String
   
    ' Change all spaces to pluses, and escape
    ' anything not A-Z or a-z
   
    For i = 1 To Len(sDecoded)
        Char = Mid(sDecoded, i, 1)
        Ascii = Asc(Char)
        If Ascii = 32 Then
            sOutput = sOutput + "+"
        ElseIf (Ascii < 65 Or Ascii > 122) And (Ascii < 48 Or Ascii > 57) Then
            TwoHex = Hex$(Ascii)
            If Len(TwoHex) = 1 Then TwoHex = "0" + TwoHex
            sOutput = sOutput + "%" + TwoHex
        Else
            sOutput = sOutput + Char
        End If
    Next i
   
    ' return encoded string to caller
    URLEncode = sOutput
End Function
'-----------------------




And the URLDecode function:
'-----------------------
Public Function URLDecode(sEncoded As String) As String
    'This function decodes a URL-encoded string
    Dim x As Integer, i As Integer
   
    'Check if the string is empty
    If sEncoded = "" Then Exit Function

   
    ' first fix all the plus signs
    Do
        x = InStr(x + 1, sEncoded, "+")
        If x = 0 Then Exit Do
        Mid(sEncoded, x, 1) = " "
    Loop
       
    ' now fix the %xx escapes
    On Error GoTo errorUrlDecode
    Do
        x = InStr(x + 1, sEncoded, "%")
        If x = 0 Then Exit Do
        i = Val("&H" + Mid(sEncoded, x + 1, 2))
        If i Then sEncoded = Left(sEncoded, x - 1) + Chr$(i) + Mid(sEncoded, x + 3)
    Loop
    URLDecode = sEncoded
Exit Function

errorUrlDecode:
    '--------------------------------------------------------------------
    ' If this function was mistakenly called with the following:
    '    UrlDecode("100% natural")
    ' a type mismatch error would be raised when trying to convert
    ' the 2 characters after "%" from hex to character.
    ' Instead, a more descriptive error message will be generated.
    '--------------------------------------------------------------------
    If Err.Number = 13 Then      'Type Mismatch error
        Err.Clear
        Err.Raise 65001, , "Invalid data passed to UrlDecode() function."
    Else
        Err.Raise Err.Number
    End If
    Resume Next
End Function
'-----------------------
0
 
vbyuvalCommented:
I can build encoder with more than one encode format for you .
You will get it hour after I will see some pts !
0
 
sivanpAuthor Commented:
Thanx a lot.
Thats exactly what I searched for.
0
All Courses

From novice to tech pro — start learning today.