Sign in to follow this  
Followers 0
nullschritt

URL Encoding

7 posts in this topic

#1 ·  Posted (edited)

I was just wondering how I would go about encoding a string for http transport (encoding the special characters like "#$%&=", etc?)

Thanks

Edited by nullschritt

Share this post


Link to post
Share on other sites



Do a search on base64 encoding.


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

Share this post


Link to post
Share on other sites

Do a search on base64 encoding.

I don't want to encode the whole string just special characters.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
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

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