Jump to content

URL Encoding


Recommended Posts

Then you'll have to make up your own identifiers like __AMPERSAND__ or something and do some string replacing on both ends.

edit: Wait I forgot about url encoding, still same priciple with string replacing.

Edited by dany

[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

Link to comment
Share on other sites

My solution: (not tested yet, going to test later)

In case anyone else was wondering the same thing

func _urlencode($string)
$string = StringSplit($string, "")
for $i=1 to $string[0]
If chrw($string[$i]) < 48 or chrw($string[$i]) > 122
$string[$i] = "%"&_StringToHex($string[$i])
EndIf
Next
$string = _ArrayToString($string, "")
Return $string
EndFunc


func _urldecode($string)
$string = StringSplit($string, "%")
for $i=1 to $string[0]
$string[$i] = _HexToString(StringLeft($string[$i], 2))&StringTrimLeft($string[$i], 2)
Next
$string = _ArrayToString($string, "")
Return $string
EndFunc
Edited by nullschritt
Link to comment
Share on other sites

Almost :) You used ChrW where AscW is needed and forgot a string check for hexadecimals. Fixed below.

Do a forum search on 'urlencode', you'll get some pretty interresting topics.

#include <String.au3>
#include <Array.au3>

Func _urlencode($string)
    $string = StringSplit($string, "")
    For $i=1 To $string[0]
        If AscW($string[$i]) < 48 Or AscW($string[$i]) > 122 Then
            $string[$i] = "%"&_StringToHex($string[$i])
        EndIf
    Next
    $string = _ArrayToString($string, "", 1)
    Return $string
EndFunc


Func _urldecode($string)
    $string = StringSplit($string, "%")
    For $i=1 To $string[0]
        If StringIsXDigit(StringLeft($string[$i], 2)) Then ; Otherwise you get stray 0x in the string.
            $string[$i] = _HexToString(StringLeft($string[$i], 2))&StringTrimLeft($string[$i], 2)
        EndIf
    Next
    $string = _ArrayToString($string, "", 1)
    Return $string
EndFunc

; Using w3school example.
Local $sUrl = 'Hello Günter'
Local $sEnc = _urlencode($sUrl)
MsgBox(0, '', $sEnc)
Local $sDec = _urldecode($sEnc)
MsgBox(0, '', $sDec)
Edited by dany

[center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF

Link to comment
Share on other sites

Here is my take on the functions

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...