Sign in to follow this  
Followers 0
DreamVB

Url Encoder / Decoder functions

2 posts in this topic

hi here are two little functions I made to encode and decode web URL strings, any chars that are not valid are converted to hexadecimal and vice versa, Well I wrapped it up with a little GUI for you to test hope it maybe useful.

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Local Const $AppTitle = "URL Tools"

Func DecodeUrl($src)
    Local $i
    Local $ch
    Local $buff

    ;Init Counter
    $i = 1

    While ($i <= StringLen($src))
        $ch = StringMid($src, $i, 1)
        ;Correct spaces
        If ($ch = "+") Then
            $ch = " "
        EndIf
        ;Decode any hex values
        If ($ch = "%") Then
            $ch = Chr(Dec(StringMid($src, $i + 1, 2)))
            $i += 2
        EndIf
        ;Build buffer
        $buff &= $ch
        ;Inc Counter
        $i += 1
    WEnd

    Return $buff
EndFunc   ;==>DecodeUrl

Func EncodeUrl($src)
    Local $i
    Local $ch
    Local $NewChr
    Local $buff

    ;Init Counter
    $i = 1

    While ($i <= StringLen($src))
        ;Get byte code from string
        $ch = Asc(StringMid($src, $i, 1))

        ;Look for what bytes we have
        Switch $ch
            ;Looks ok here
            Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126
                $buff &= Chr($ch)
                ;Space found
            Case 32
                $buff &= "+"
            Case Else
                ;Convert $ch to hexidecimal
                $buff &= "%" & Hex($ch, 2)
        EndSwitch
        ;INC Counter
        $i += 1
    WEnd

    Return $buff
EndFunc   ;==>EncodeUrl

Func ShowForm()
    Local $Data
    ;Create form controls
    $frmmain = GUICreate($AppTitle, 509, 265, 314, 360)
    $lblTitle1 = GUICtrlCreateLabel("URL Decoder/Encoder", 8, 7, 115, 17)
    $cmdEncode = GUICtrlCreateButton("&Encode", 423, 26, 75, 25)
    $cmdExit = GUICtrlCreateButton("E&xit", 423, 132, 75, 25)
    $txtData = GUICtrlCreateEdit("Testing + 12 http://www.google.com", 8, 26, 409, 193)
    $cmdDecode = GUICtrlCreateButton("&Decode", 423, 57, 75, 25)
    $cmdAbout = GUICtrlCreateButton("&About", 423, 100, 75, 25)
    $lblDesc = GUICtrlCreateLabel("Ben's URL Encoder/Decoder v1.0", 90, 228, 243, 24)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlSetColor(-1, 0x000080)

    ;Show form
    GUISetState(@SW_SHOW)

    ;Form Messages loop
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $cmdExit
                Exit
            Case $cmdEncode
                ;Get text data
                $Data = _GUICtrlEdit_GetText($txtData)
                ;Check length
                If StringLen($Data) > 0 Then
                    ;Place text into decoded text box.
                    _GUICtrlEdit_SetText($txtData, EncodeUrl($Data))
                EndIf
            Case $cmdDecode
                ;Get text data
                $Data = _GUICtrlEdit_GetText($txtData)
                ;Check length
                If StringLen($Data) > 0 Then
                    ;Place text into decoded text box.
                    _GUICtrlEdit_SetText($txtData, DecodeUrl($Data))
                EndIf
            Case $cmdAbout
                MsgBox(64, "About", $AppTitle & " v1.0" & @CRLF & @TAB & "By Ben Jones")
        EndSwitch
    WEnd
EndFunc   ;==>ShowForm

;Load main form
ShowForm()
2 people like this

On Error Resume Pulling Hair Out.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Thanks for this piece of code, it was useful to me :)
 
 
old post:
Is the code only broken for me? I got a --URL::5e1cd925cceeb0b8f1f0f4b9c11e2e45-- or similar before each line that isn't a comment, what's up with that?
 
Edit: As soon as I posted, it vanished...
 
Edit 2: So I wasn't crazy, it's the same on this thread: '?do=embed' frameborder='0' data-embedContent>>
 
Edit 3: Okay, now I got it... It's the highlighting: ?hl=%2Burl+%2Bdecode'>?hl=%2Burl My bad.

Edited by Miranda

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0