Jump to content

URL Encoding

Recommended Posts

Ive got this string: 123abc!@#$%^&*()_+

If I run it through a POST request on a website, it converts it to: 123abc%21%40%23%24%25%5E%26*%28%29_%2B+

What encoding is it?

Link to comment
Share on other sites

Had this snippet, don't know from where :), Kudos to the author!

ConsoleWrite(URLEncode("123abc!@#$%^&*()_+ ") & @crlf)

Func URLEncode($urlText)
    $url = ""
    For $i = 1 To StringLen($urlText)
        $acode = Asc(StringMid($urlText, $i, 1))
            Case ($acode >= 48 And $acode <= 57) Or _
                    ($acode >= 65 And $acode <= 90) Or _
                    ($acode >= 97 And $acode <= 122)
                $url = $url & StringMid($urlText, $i, 1)
            Case $acode = 32
                $url = $url & "+"
            Case Else
                $url = $url & "%" & Hex($acode, 2)
    Return $url
EndFunc   ;==>URLEncode

Edit: I bet URL encoding is defined in some standard, but don't know which. I was the result of my function does not match yours to the point, * and _ didn't seem to be converted by your script (to emulate maybe explicitly exclude these characters from the URLEncode() function).

Edit2: Guess it's something like this http://en.wikipedia.org/wiki/Percent-encoding

Edited by KaFu
Link to comment
Share on other sites

  • 2 months later...

I wrote some functions including UTF-8 conversion :(

Func _URIEncode($sData)
    ; Prog@ndy
    Local $aData = StringSplit(BinaryToString(StringToBinary($sData,4),1),"")
    Local $nChar
    For $i = 1 To $aData[0]
        ConsoleWrite($aData[$i] & @CRLF)
        $nChar = Asc($aData[$i])
        Switch $nChar
            Case 45, 46, 48-57, 65 To 90, 95, 97 To 122, 126
                $sData &= $aData[$i]
            Case 32
                $sData &= "+"
            Case Else
                $sData &= "%" & Hex($nChar,2)
    Return $sData

Func _URIDecode($sData)
    ; Prog@ndy
    Local $aData = StringSplit(StringReplace($sData,"+"," ",0,1),"%")
    $sData = ""
    For $i = 2 To $aData[0]
        $aData[1] &= Chr(Dec(StringLeft($aData[$i],2))) & StringTrimLeft($aData[$i],2)
    Return BinaryToString(StringToBinary($aData[1],1),4)

MsgBox(0, '', _URIDecode(_URIEncode("testäöü fv")))


My question may sound stupid, but how do I know whether string needs to be decoded or encoded?

let me explain, I am taking part string from an URL box of IE automaticaly (Actually, its a value of one of the get method variables)

and wants to know if this string is decoded or encoded. E.g. Sometimes this values come in this form; %E0%E1%F8%E4%ED#876 and some times its just a UTF string (locale specific string that can be read by human).

For example:



Are there any functions such as IsEncodedURI or IsDecodedURI?

See the "q=" parameter

BTW: Your code is wonderful. Thanks for sharing it. >_<

Edited by lsakizada

Be Green Now or Never (BGNN)!

Link to comment
Share on other sites

  • 2 years later...

ą, ń, ę, ż, dż, ź, ó, ł, ś, ć, ( from file ) ---> ±, ę, ż, dż, Ľ, ó, ł, ¶, ć, ( from url )

How to change this? : ±, ę, ż, dż, Ľ, ó, ł, ¶, ć, ---> ą, ń, ę, ż, dż, ź, ó, ł, ś, ć,


The text is encoded in ISO8859-2. If you want, you can use the following function to get the text in ANSI (works only for Polish):

[ code='text' ] ( Popup )
Func ISOtoANSI($sText)
; http://en.wikipedia.org/wiki/Polish_code_pages
$sText = StringReplace($sText, Chr(Dec("A1")), "Ą")
$sText = StringReplace($sText, Chr(Dec("A6")), "Ś")
$sText = StringReplace($sText, Chr(Dec("AC")), "Ź")
$sText = StringReplace($sText, Chr(Dec("B1")), "ą")
$sText = StringReplace($sText, Chr(Dec("B6")), "ś")
$sText = StringReplace($sText, Chr(Dec("BC")), "ź")
Return $sText
EndFunc   ;==>ISOtoANSI
Edited by tele123
Link to comment
Share on other sites

  • 7 months later...

Here is my rulencode and urldecode functions. UTF8 is supported and functions are compliant with RFC 3986. The urlencode function accepts a seccond parameter - if true (default) it will encode spaces as plus. If false - the space will be encoded as "%20" (not compliant)

Func urlencode($str, $plus = True)
    Local $i, $return, $tmp, $exp
    $return = ""
    $exp = "[a-zA-Z0-9-._~]"
    If $plus Then
        $str = StringReplace ($str, " ", "+")
        $exp = "[a-zA-Z0-9-._~+]"
    For $i = 1 To StringLen($str)
        $tmp = StringMid($str, $i, 1)
        If StringRegExp($tmp, $exp, 0) = 1 Then
            $return &= $tmp
            $return &= StringMid(StringRegExpReplace(StringToBinary($tmp, 4), "([0-9A-Fa-f]{2})", "%$1"), 3)
    Return $return

Func urldecode($str)
    Local $i, $return, $tmp
    $return = ""
    $str = StringReplace ($str, "+", " ")
    For $i = 1 To StringLen($str)
        $tmp = StringMid($str, $i, 3)
        If StringRegExp($tmp, "%[0-9A-Fa-f]{2}", 0) = 1 Then
            $i += 2
            While StringRegExp(StringMid($str, $i+1, 3), "%[0-9A-Fa-f]{2}", 0) = 1
                $tmp = $tmp & StringMid($str, $i+2, 2)
                $i += 3
            $return &= BinaryToString(StringRegExpReplace($tmp, "%([0-9A-Fa-f]*)", "0x$1"), 4)
            $return &= StringMid($str, $i, 1)
    Return $return
Link to comment
Share on other sites


This would have been best suited in the examples section and not digging up an old post such as this. Secondly, the best version I've seen has be ProgAndy's which features in the WinHTTP UDF. Thanks for your contribution and welcome to the Forum.

UDF List:

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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

  • Recently Browsing   0 members

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