How to make strings portable for transmitting?
;==========================================================================================================================================
;
; How to make strings portable for transmitting via network using encode and decode functions.
;
;==========================================================================================================================================


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrEscape (strString)
objSC = CreateObject ("MSScriptControl.ScriptControl")
objSC.Language = "JScript"
Return objSC.Eval(: 'escape("' : StrReplace (strString, '"', '\"') : '")')
;..........................................................................................................................................
; This UDF "udfStrEscape" converts ASCII control characters (ASCII value 0..31)
; and special characters, e. g. German Umlaut characters, into hexadecimal numbers of format %xx,
; e. g. " " --> "%20" (percent sign and two hex digits).
;
; This function makes a string portable, so it can be transmitted across any network to any computer that supports ASCII characters.
; Characters [*@-_+./] will not be converted.
;
; From:  stanl slittlefield1@gmail.com
; Date:  Saturday, January 16, 2010 03:43 AM
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrUnEscape (strString)
objSC = CreateObject ("MSScriptControl.ScriptControl")
objSC.Language = "JScript"
Return objSC.Eval(: 'unescape("' : strString : '")')
;..........................................................................................................................................
; This UDF "udfStrUnEscape" converts hexadecimal characters (formerly encoded by udfStrEscape())
; back into ASCII characters, e. g. "%20" --> " ".
;
; Note: The udfStrUnescape() function should not be used to decode URIs. Use udfStrDecodeURI() function instead.
;
; From:  stanl slittlefield1@gmail.com
; Date:  Saturday, January 16, 2010 03:43 AM
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrEncodeURI (strString)
objSC = CreateObject ("MSScriptControl.ScriptControl")
objSC.Language = "JScript"
Return objSC.Eval(: 'encodeURI("' : StrReplace (strString, '"', '\"') : '")')
;..........................................................................................................................................
; This UDF "udfStrEncodeURI" converts special characters into hexadecimal numbers of format %hex, e. g. " " --> "%20",
; useful for creating "Universal Resource Identifier (URI)" strings.
;
; Characters [a-zA-Z0-9-_.!~*'()] will not be converted.
; Special characters [,/?:@&=+$#] will not be converted (use encodeURIComponent() to encode these characters).
; Some special character will be converted to multibyte UTF-8 character sequence as needed, e. g. "ä" --> "%C3%A4".
; Use the udfStrDecodeURI() function to decode an encoded URI.
;
; Detlev Dalitz.20100116.
..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrDecodeURI (strString)
objSC = CreateObject ("MSScriptControl.ScriptControl")
objSC.Language = "JScript"
Return objSC.Eval(: 'decodeURI("' : strString : '")')
;..........................................................................................................................................
; This UDF "udfStrDecodeURI" converts characters which are encoded for usage in "Universal Resource Identifier (URI)" strings
; (formerly encoded by udfStrEncodeURI()) into their human readable equivalent, e. g. "%C3%A4" --> "ä".
;
; Detlev Dalitz.20100116.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrEncodeURIComp (strString)
objSC = CreateObject ("MSScriptControl.ScriptControl")
objSC.Language = "JScript"
Return objSC.Eval(: 'encodeURIComponent("' : StrReplace (strString, '"', '\"') : '")')
;..........................................................................................................................................
; This UDF "udfStrEncodeURIComp" converts special characters into hexadecimal numbers of format %xx,
; e. g. " " --> "%20" (percent sign and two hex digits), useful for creating "Universal Resource Identifier (URI)" strings.
;
; Characters [a-zA-Z0-9-_.!~*'()] will not be converted.
; Special characters [,/?:@&=+$#] will be converted.
; Some special character will be converted to multibyte UTF-8 character sequence as needed, e. g. "ä" --> "%C3%A4".
; Use the udfStrDecodeURIComp() function to decode an encoded URI.
;
; Detlev Dalitz.20100116.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrDecodeURIComp (strString)
objSC = CreateObject ("MSScriptControl.ScriptControl")
objSC.Language = "JScript"
Return objSC.Eval(: 'decodeURIComponent("' : strString : '")')
;..........................................................................................................................................
; This UDF "udfStrDecodeURIComp" converts characters which are encoded for usage in "Universal Resource Identifier (URI)" strings
; (formerly encoded by udfStrEncodeURIComp()) into their human readable equivalent, e. g. "%C3%A4" --> "ä".
;
; Detlev Dalitz.20100116.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrUrlEncode (strString)
AddExtender ("WWINT44I.DLL")
Return iUrlEncode (strString)
;..........................................................................................................................................
; This UDF "udfUrlEncode" converts a plain text string into URL encoded format, e. g. " " --> "%20" (percent sign and two hex digits).
;
; Detlev Dalitz.20100116.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrUrlDecode (strString)
AddExtender ("WWINT44I.DLL")
Return iUrlDecode (strString)
;..........................................................................................................................................
; This UDF "udfUrlDecode" converts a string in URL encoded format into plain text, e. g. "%20" --> " ".
;
; Detlev Dalitz.20100116.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReplaceWithHex (strString)
intLen = StrLen (strString)
If !intlen Then Return ""
strOut = ""
For intPos = 1 To intLen
   strChar = StrSub (strString, intPos, 1)
   If !StrIndex ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", StrUpper (strChar), 1, @FWDSCAN)
      intNum = Char2Num (strChar)
      strChar = "%%" : StrSub ("0123456789ABCDEF", (intNum / 16) + 1, 1) : StrSub ("0123456789ABCDEF", (intNum mod 16) + 1, 1)
   EndIf
   strOut = strOut : strChar
Next
Return strOut
;..........................................................................................................................................
; This UDF "udfStrReplaceWithHex" converts ASCII characters into hexadecimal numbers of format %xx,
; e. g. " " --> "%20" (percent sign and two hex digits).
;
; Characters [a-zA-Z0-9] will not be converted.
;
; From : akreutzer alan.kreutzer@state.or.us
; Date : Friday, January 15, 2010 08 : 47 AM
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrEncodeUrl (strString)
If strString == "" Then Return ""
strGoodChars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ;?:=&$!*()" ; without /,'+-_.@
strBadChars = StrClean (strString, strGoodChars, "", @TRUE, 1)
If !!StrIndex (strBadChars, "%%", 1, @FWDSCAN)
   strString = StrReplace (strString, "%%", "%%25") ; The percent sign, hex encoded.
   strBadChars = StrReplace (strBadChars, "%%", "")
EndIf
While strBadChars != ""
   strChar = StrSub (strBadChars, 1, 1)
   intByte = Char2Num (strChar)
   strCharEnc = "%%" : Num2Char ((intByte >> 4) + 48 + 7 * ((intByte >> 4) > 9)) : Num2Char ((intByte & 15) + 48 + 7 * ((intByte & 15) > 9))
   strString = StrReplace (strString, strChar, strCharEnc)
   strBadChars = StrReplace (strBadChars, strChar, "")
EndWhile
Return strString
;..........................................................................................................................................
; This UDF "udfStrEncodeUrl" converts special characters into hexadecimal numbers of format %xx,
; e. g. " " --> "%20" (percent sign and two hex digits), useful for creating "Universal Resource Identifier (URI)" strings.
;..........................................................................................................................................
; strGoodChars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ;/?:@=&$-_.+!*'()," ; including /,'+
; strGoodChars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ;?:@=&$-_.!*()" ; without /,'+
;..........................................................................................................................................
; Note:
; Following modified reduced set of strGoodChars can be used for converting of email adresses. (DD.20080213)
; strGoodChars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ;?:=&$!*()" ; without /,'+-_.@
; Such encoded string cannot be decoded with JScript decodeURI().
;
; Detlev Dalitz.20040518.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrDecodeUrl (strString)
If strString == "" Then Return ""
intPos = 0
While @TRUE
   intPos = StrIndex (strString, "%%", 1 + intPos, @FWDSCAN)
   If !intPos Then Break
   strHi = StrSub (strString, intPos + 1, 1)
   strLo = StrSub (strString, intPos + 2, 1)
   intHi = Char2Num (StrUpper (strHi)) - 48
   intLo = Char2Num (StrUpper (strLo)) - 48
   strChar = Num2Char (((intHi - 7 * (intHi > 9)) << 4) + (intLo - 7 * (intLo > 9)))
   strString = StrReplace (strString, "%%" : strHi : strLo, strChar)
EndWhile
Return strString
;..........................................................................................................................................
; Detlev Dalitz.20040518.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strTest = '"Read & write" + send email to: Hans-André Schälm-Büßer@DOMAIN.COM'

strOut11 = udfStrReplaceWithHex (strTest)
strOut12 = udfStrUnEscape (strOut11)

strOut21 = udfStrEscape (strTest)
strOut22 = udfStrUnEscape (strOut21)

strOut31 = udfStrUrlEncode (strTest)
strOut32 = udfStrUrlDecode (strOut31)

strOut41 = udfStrEncodeURI (strTest)
strOut42 = udfStrDecodeURI (strOut41)

strOut51 = udfStrEncodeURIComp (strTest)
strOut52 = udfStrDecodeURIComp (strOut51)
strOut53 = udfStrUnEscape (strOut51)
strOut54 = udfStrDecodeURI (strOut51)

strOut61 = udfStrEncodeUrl (strTest)
strOut62 = udfStrDecodeUrl (strOut61)
strOut63 = udfStrUnEscape (strOut61)


; Some error retrieving, just for scripting fun.
strFileIni = ShortCutDir ("Appdata", 0, @TRUE) : "WinBatch\Settings\WWWBATCH.INI"
blnResult = FileDelete (strFileIni)
intLastEM = ErrorMode (@OFF)
LastError ()

strOut64 = udfStrDecodeURI (strOut61) ; This gives runtime error in JScript. Encryption not valid.

intLastError = LastError ()
ErrorMode (intLastEM)
Drop (intLastEM)
If intLastError > 0
   strFileIni = ShortCutDir ("Appdata", 0, @TRUE) : "WinBatch\Settings\WWWBATCH.INI"
   strKey = IniItemizePvt ("COM Exception", strFileIni)
   strValue = IniReadPvt ("COM Exception", strKey, "", strFileIni)
   strOut64 = wberrorarray[0] : '|"' : wberrorarray[5] : '"|"' : strKey : "=" : strValue : '"'
EndIf


; Create output.
strLog = "Output Log .........: " : TimeYmdHms () : @LF
strLog = strLog : @LF : "strTest . ..........: " : strTest
strLog = strLog : @LF : @LF : "udfStrReplaceWithHex: " : strOut11
strLog = strLog : @LF : "udfStrUnEscape .....: " : strOut12
strLog = strLog : @LF : @LF : "udfStrEscape .......: " : strOut21
strLog = strLog : @LF : "udfStrUnEscape .....: " : strOut22
strLog = strLog : @LF : @LF : "udfStrUrlEncode ....: " : strOut31
strLog = strLog : @LF : "udfStrUrlDecode ....: " : strOut32
strLog = strLog : @LF : @LF : "udfStrEncodeURI ....: " : strOut41
strLog = strLog : @LF : "udfStrDecodeURI ....: " : strOut42
strLog = strLog : @LF : @LF : "udfStrEncodeURIComp : " : strOut51
strLog = strLog : @LF : "udfStrDecodeURIComp : " : strOut52
strLog = strLog : @LF : "udfStrUnEscape .....: " : strOut53
strLog = strLog : @LF : "udfStrDecodeURI ....: " : strOut54
strLog = strLog : @LF : @LF : "udfStrEncodeUrl ....: " : strOut61
strLog = strLog : @LF : "udfStrDecodeUrl ....: " : strOut62
strLog = strLog : @LF : "udfStrUnEscape .....: " : strOut63
strLog = strLog : @LF : "udfStrDecodeURI ....: " : strOut64


strFileOut = DirScript () : StrInsert (StrReplace (TimeYmdHms (), ":", ""), ".", "", 9, 1) : ".FileOut.txt"
intBytesWritten = FilePut (strFileOut, StrReplace (strLog, @LF, @CRLF))
RunWait (strFileOut, "")
blnResult = FileDelete (strFileOut)

Exit