AutoHotkey Community

It is currently May 27th, 2012, 1:13 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: June 13th, 2008, 6:16 am 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
I'm trying to get together a bunch of encoding and decoding functions for easy reference. So if you've got some functions for this kind of stuff please, share!

I originally had the goal of making this a lib, but I can't think of any prefixes that aren't stupid, so I guess it'll just be a resource. :S :P

This is what we have so far:
Code:
;#########################################################################################
;XML encode/decode by infogulch  -  this might be handy for use with xpath by titan
;About: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

Dec_XML(str)
{ ;Decode xml required characters, as well as numeric character references
   Loop
      If RegexMatch(str, "S)(&#(\d+);)", dec)                  ; matches:   &#[dec];
         StringReplace, str, str, %dec1%, % Chr(dec2), All
      Else If   RegexMatch(str, "Si)(&#x([\da-f]+);)", hex)         ; matches:   &#x[hex];
         StringReplace, str, str, %hex1%, % Chr("0x" . hex2), All
      Else
         Break
   StringReplace, str, str,  , %A_Space%, All
   StringReplace, str, str, &quot;, ", All         ;required predefined character entities &"<'>
   StringReplace, str, str, &apos;, ', All
   StringReplace, str, str, &lt;,   <, All
   StringReplace, str, str, &gt;,   >, All
   StringReplace, str, str, &amp;,  &, All         ;do this last so str doesn't resolve to other entities
   return, str
}

Enc_XML(str, chars="")
{ ;encode required xml characters. and characters listed in Param2 as numeric character references
   StringReplace, str, str, &, &amp;,  All         ;do first so it doesn't re-encode already encoded characters
   StringReplace, str, str, ", &quot;, All         ;required predefined character entities &"<'>
   StringReplace, str, str, ', &apos;, All
   StringReplace, str, str, <, &lt;,   All
   StringReplace, str, str, >, &gt;,   All
   Loop, Parse, chars         
      StringReplace, str, str, %A_LoopField%, % "&#" . Asc(A_LoopField) . "`;", All
   return, str
}

;#########################################################################################
;uri encode/decode by Titan
;Thread: http://www.autohotkey.com/forum/topic18876.html
;About: http://en.wikipedia.org/wiki/Percent_encoding
;two functions by titan: (slightly modified by infogulch)

Dec_Uri(str)
{
   Loop
      If RegExMatch(str, "i)(?<=%)[\da-f]{1,2}", hex)
         StringReplace, str, str, `%%hex%, % Chr("0x" . hex), All
      Else Break
   Return, str
}

Enc_Uri(str)
{
   f = %A_FormatInteger%
   SetFormat, Integer, Hex
   If RegExMatch(str, "^\w+:/{0,2}", pr)
      StringTrimLeft, str, str, StrLen(pr)
   StringReplace, str, str, `%, `%25, All
   Loop
      If RegExMatch(str, "i)[^\w\.~%/:]", char)
         StringReplace, str, str, %char%, % "%" . SubStr(Asc(char),3), All
      Else Break
   SetFormat, Integer, %f%
   Return, pr . str
}


;#########################################################################################
; - String2hex, Laszlo (there are various versions (hexify) on the forum
;   http://www.autohotkey.com/forum/topic4934-15.html#158672
;   search the forum, also by Rajat if you are interested)
;   I just fiddled around a bit until I got the results I wanted :-)
;   Not all hexify version work with ASCII 1 - 15, this one does

Enc_Hex(x) ;originally: String2Hex(x) ; Convert a string to hex digits (modified to accommodate new line chars)
{
   prevFmt = %A_FormatInteger%
   SetFormat Integer, H ; this function requires hex format
   Loop Parse, x
      hex .= 0x100+Asc(A_LoopField)
   StringReplace hex, hex, 0x1,,All
   SetFormat Integer, %prevFmt% ; restore original integer formatting
   Return hex
}

Dec_Hex(x) ;originally: Hex2String(x) ; Convert a huge hex number to string (modified to suit String2Hex above)
{
   Loop % StrLen(x)/2
   {
      Pos:=(A_Index-1)*2+1
      StringMid hex, x, Pos, 2
      string := string . Chr("0x" hex)
   }
   Return string
}


We also have some fun stuff:
Code:
; --------------------------------------------------------------------------
; Encode/Decode Albed (final fantasy)
; example
Text=Hello Word!
Text:=Enc_Albed(Text)
MsgBox % "Albed: " Text
Text:=Dec_Albed(Text)
MsgBox % "English: " Text

; Reference http://www.autohotkey.com/forum/topic30667.html

Enc_Albed(String)
{
StringCaseSenseSetting:=A_StringCaseSense
StringCaseSense Off
Albed=ypltavkrezgmshubxncdijfoqw
Loop, Parse, String
   {
   Ascii:=Asc(A_LoopField)
   If A_LoopField in A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
      {
      Replace:=SubStr(Albed, Ascii-96,1)
      If (Ascii > 64) AND (Ascii < 91) ; upper case character
         {
         Replace:=SubStr(Albed, Ascii-64,1)
         StringUpper,Replace,Replace
         }
      TranslatedString:=TranslatedString . Replace
      }
      Else
         TranslatedString:=TranslatedString . A_LoopField
   }
StringCaseSense %StringCaseSenseSetting%
Return TranslatedString
}

Dec_Albed(String)
{
StringCaseSenseSetting:=A_StringCaseSense
StringCaseSense Off
Albed=ypltavkrezgmshubxncdijfoqw
Loop, Parse, String
   {
   Ascii:=Asc(A_LoopField)
   If A_LoopField in A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
   {
   Replace:=Chr(InStr(Albed, A_LoopField)+96)
   If (Ascii > 64) AND (Ascii < 91) ; upper case character
      {
      Replace:=Chr(InStr(Albed, A_LoopField)+64)
      StringUpper,Replace,Replace
      }
   TranslatedString:=TranslatedString . Replace
   }
      Else
      TranslatedString:=TranslatedString . A_LoopField
   }
StringCaseSense %StringCaseSenseSetting%
Return TranslatedString
}



Any additions/suggestions/comments etc. appreciated! Thanks for looking. :)

_________________
Scripts - License


Last edited by infogulch on July 28th, 2008, 6:43 pm, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 17th, 2008, 4:46 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
Updated. I added an XML encode function, and updated the XML decode function.

Does anyone find this useful?

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 22nd, 2008, 2:36 pm 
Offline

Joined: July 22nd, 2008, 2:33 pm
Posts: 7
Thanks a million. I was working with XML and the decXML worked like a charm to convert #44; back into commas for proper display.

8)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 25th, 2008, 6:12 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
you're welcome. it will also fix other references as well, so you don't have to worry about surprise characters popping up that aren't decoded. :D

Updated. Fixed broken Enc_XML(). Looks like I'm not getting a bunch more functions to add, so I'll change it to 1.0.

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 28th, 2008, 4:10 pm 
Offline

Joined: July 28th, 2008, 3:46 pm
Posts: 1
Location: Denver area
infogulch wrote:
I'm trying to get together a bunch of encoding and decoding functions for easy reference. So if you've got some functions for this kind of stuff please, share!

I originally had the goal of making this a lib, but I can't think of any prefixes that aren't stupid, so I guess it'll just be a resource. :S :P



We also have some fun stuff:
Code:
; --------------------------------------------------------------------------
; Encode/Decode Albed (final fantasy)
; example
Text=Hello Word!
Text:=Encode_Albed(Text)
MsgBox % "Albed: " Text
Text:=Decode_Albed(Text)
MsgBox % "English: " Text

; Reference http://www.autohotkey.com/forum/topic30667.html

Enc_Albed(String)
{
StringCaseSenseSetting:=A_StringCaseSense
StringCaseSense Off
Albed=ypltavkrezgmshubxncdijfqrw
Loop, Parse, String
   {
   Ascii:=Asc(A_LoopField)
   If A_LoopField in A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
      {
      Replace:=SubStr(Albed, Ascii-96,1)
      If (Ascii > 64) AND (Ascii < 91) ; upper case character
         {
         Replace:=SubStr(Albed, Ascii-64,1)
         StringUpper,Replace,Replace
         }
      TranslatedString:=TranslatedString . Replace
      }
      Else
         TranslatedString:=TranslatedString . A_LoopField
   }
StringCaseSense %StringCaseSenseSetting%
Return TranslatedString
}

Dec_Albed(String)
{
StringCaseSenseSetting:=A_StringCaseSense
StringCaseSense Off
Albed=ypltavkrezgmshubxncdijfqrw
Loop, Parse, String
   {
   Ascii:=Asc(A_LoopField)
   If A_LoopField in A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
   {
   Replace:=Chr(InStr(Albed, A_LoopField)+96)
   If (Ascii > 64) AND (Ascii < 91) ; upper case character
      {
      Replace:=Chr(InStr(Albed, A_LoopField)+64)
      StringUpper,Replace,Replace
      }
   TranslatedString:=TranslatedString . Replace
   }
      Else
      TranslatedString:=TranslatedString . A_LoopField
   }
StringCaseSense %StringCaseSenseSetting%
Return TranslatedString
}



Any additions/suggestions/comments etc. appreciated! Thanks for looking. :)


Nice program!!! I did find 2 minor glitches in the code listed:

program calls subroutines Encode_Albed and Decode_Albed but actual tags are Enc_Albed and Dec_Albed.

Also found the string "Albed=ypltavkrezgmshubxncdijfqrw" has r used twice and o is not used casusing errors for the english y.

Going to quoted thread to ask questions...

P.S. found on quoted thread the string was given to HugoV by the original requestor (etopsirhc) with the two r's so it's not his fault.......


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 28th, 2008, 4:42 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
OMG some actually wants to use it :-)
logic wrote:
Also found the string "Albed=ypltavkrezgmshubxncdijfqrw" has r used twice and o is not used casusing errors for the english y.


According to this http://www.geocities.com/juewuming/abc.html the correct
string should be
Code:
ypltavkrezgmshubxncdijfoqw


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 28th, 2008, 6:42 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
Update 1.1: Fixed test function calls and correct albed string for the Enc/Dec_Albed functions.

Thanks guys. :)

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
PostPosted: August 9th, 2008, 12:15 am 
Offline

Joined: August 8th, 2008, 1:40 am
Posts: 36
HugoV wrote:
OMG some actually wants to use it :-)
logic wrote:
Also found the string "Albed=ypltavkrezgmshubxncdijfqrw" has r used twice and o is not used casusing errors for the english y.


According to this http://www.geocities.com/juewuming/abc.html the correct
string should be
Code:
ypltavkrezgmshubxncdijfoqw


I'm Logic but couldn't remember what my login and password and email was so I had to re-register...

I actually wanted to use AHK to do Lewis Carroll's Cipher. Back in the early 80's when I did a lot of programming, I wanted to automate the encription/decription function and spent years trying to figure out how to create the array in memory and acutally use it. I was doing some other programming and using an ascii conversion sheet to look something up and suddenly it dawned on me I could convert both plaintext and key to their ascii numbers, add them together, then convert back to alpha. I then wrote the code in minutes and used it quite a bit my senior year....

Just two years ago, I found a basic program submitted to the web ten years earlier where someone actually wrote a program that did what I was trying to do, it worked the encription/decription via an array built into memory. I was tempted to write him and tell him he did what I couldn't figure out, but what I figured out was much, much more elegant.

I was trying to figure out exatly what you did so I could write my script, but work has been a nightmare lately and most likely will continue for the rest of the year, so I won't have time to play with it for quite some time.

I input a message and it didn't return what I put in is how I found the double r.... I never intended to play the game, I 'm not a gamer....unless I could find a PC version of "microwave" somewhere.... :lol:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 2nd, 2008, 3:38 pm 
Offline

Joined: August 8th, 2008, 1:40 am
Posts: 36
When I first found this I coundn't figure out how it worked. I figured I would figure it out eventually to use it for Lewis Carroll's cipher, but even after several hours of trying to figure it out, I couldn't grasp seveal of the concepts......

That was just three months ago....

Last week and this week I actually used this (parts and concept anyways) in a script I wrote to automate a job function. I pulled a report from a system and had to format the text (remove all spaces and replaced the first space with a comma for CSV reading... here's the script...

Code:
#EscapeChar '
Hotkey, ^p, Endrun
DataFile := "H:\My Documents\SDPreport.txt"
TempFile := "H:\My Documents\temp.txt"
InputBox, Office,,Which office would you like a SP report for?
StringUpper, Office, Office
InputBox, Type,,How would you like it filtered?
If Type = cutcode
     InputBox, CutCode,,Which cut code?
FileDelete, %DataFile%
FileDelete, %TempFile%
Run, "C:\Program Files\Internet Explorer\IEXPLORE.EXE" Http://***deleted actual URL per Corporate policy***
Sleep, 400
WinActivate, SDP - Service Path Reports - Microsoft Internet Explorer, Links
WinWaitActive, SDP - Service Path Reports - Microsoft Internet Explorer, Links
Sleep, 100
WinMove, SDP - Service Path Reports - Microsoft Internet Explorer, Links, , , 968, 896
Sleep, 100
Click 557, 250
Click 488, 363
Sleep, 200
Click 648, 249
Send, %Office%
Sleep, 100
Click 816, 248
Sleep, 200
click 951, 16
Sleep, 2000
Run, "C:\Program Files\Internet Explorer\IEXPLORE.EXE" Http://***deleted actual URL per Corporate policy***
Sleep, 400
FindReport:
LineNum := 229
Loop 4
{
     LineNum := LineNum + 26
     WinActivate, Specific Originator - Microsoft Internet Explorer, Links
     WinWaitActive, Specific Originator - Microsoft Internet Explorer, Links
     Sleep, 400
     Click 198, %LineNum%
     Sleep, 800
     WinActivate, SDP - Service Path Reports - Microsoft Internet Explorer, Links
     WinWaitActive, SDP - Service Path Reports - Microsoft Internet Explorer, Links
     Sleep, 100
     WinMove, SDP - Service Path Reports - Microsoft Internet Explorer, Links, , , 1117, 896
     Sleep, 100
     clipboard =
     Sleep, 200
     Click 237, 162, 2
     Sleep, 400
     Send, {CTRLDOWN}c{CTRLUP}
     Location := SubStr(clipboard, 1, 8)
     If Location <> %Office%
     {
          MsgBox, :%Office%:%Location%:
          Winclose,  SDP - Service Path Reports - Microsoft Internet Explorer, Links
          WinActivate, Specific Originator - Microsoft Internet Explorer, Links
          WinWaitActive, Specific Originator - Microsoft Internet Explorer, Links
          Sleep, 400
     }
     Else GoTo OfficeFound
}
Send, {F5}
Sleep, 400
Goto, FindReport
OfficeFound:
Winclose,  Specific Originator - Microsoft Internet Explorer, Links   
Sleep, 200
WinActivate, SDP - Service Path Reports - Microsoft Internet Explorer, Links
WinWaitActive, SDP - Service Path Reports - Microsoft Internet Explorer, Links
Sleep, 200
Click 200, 550
Sleep, 200
clipboard =
Sleep, 200
Send, {CTRLDOWN}ac{CTRLUP}
Sleep, 200
Winclose,  SDP - Service Path Reports - Microsoft Internet Explorer, Links
FileAppend,
(
%clipboard%
), %TempFile%
Loop, Read, %TempFile%   ;read text file one line at a time
{
     If Type = cutcode
     {
          StringRight, CutExists, A_LoopReadLine, 2
          If CutExists = %CutCode%
          {
               Converted:=Enc_Spaces(A_LoopReadLine)
               FileAppend,
               (
               'n%Converted%
               ), %DataFile%
          }
     }
     else
     {
          Converted:=Enc_Spaces(A_LoopReadLine)
          FileAppend,
          (
          'n%Converted%
          ), %DataFile%
     }
}
Sleep, 200
Run, %DataFile%
ExitApp


Enc_Spaces(String)
{
     StringCaseSenseSetting:=A_StringCaseSense
     StringCaseSense Off
          Loop, Parse, String
          {
               Test := asc(A_LoopField)
               If (Test = 32)
               {
                    If Ind = 2
                         Replace := ""
                    Else
                    {
                         Replace := ","
                    }
                    TranslatedString := TranslatedString . Replace
                    Ind = 2
               }
               Else
               {
                    TranslatedString := TranslatedString . A_Loopfield
                    Ind = 1
               }
          }
     StringCaseSense %StringCaseSenseSetting%
     Return TranslatedString
}



Endrun:
MsgBox, Script aborted...
ExitApp


Another place I used the concept to remove leading zeros (but not zeros in the middle of the number i.e. 00403 converts to 403) to compare what exists (was added) to what was requested (found on the report from above):

Code:
Action := "A"
GoSub, SVCP
Sleep, 100
Loop %Loop%
{
     Ind = 1
     Loop, Parse, SPNum%A_Index%
     {
          If Ind = 1
          {
               If A_LoopField = 0
                    NewString%A_Index% := ""
               Else
               {
                    NewString%A_Index% := A_LoopField
                    Ind = 2
               }
          }
          Else
          {
               NewString%A_Index% := A_LoopField
          }
     }
     SPNum%A_Index% := NewString1 . NewString2 . NewString3 . NewString4 . NewString5
     If (SPNum%A_Index% <> SdpNum%A_Index%)
     {
          MsgBox % "Sevice Path not added for activity " . ActNum%A_Index% . "'n" .  SPNum%A_Index% . " : " . SdpNum%A_Index%
          WinWait, ACMS - A
          IfWinNotActive, ACMS - A, , WinActivate, ACMS - A,
          WinWaitActive, ACMS - A,
     }
}


and one more where I was removing underscores so I could compare CLLI codes(Common Language Location Identification codes):

Code:
SVCPAction:
Page := 1
Loop 4
{
     clipboard =
     Sleep, 200
     MouseClickDrag, left, 13, 277, 100, 277
     Sleep, 400
     SendInput {CTRLDOWN}c{CTRLUP}
     Sleep, 200
     Location := clipboard
     Location:=Enc_Underscore(Location)
     If Location contains %EchoOffice%
          SendInput %Action%
     Else
     {
          clipboard =
          Sleep, 200
          MouseClickDrag, left, 350, 277, 433, 277
          Sleep, 400
          SendInput {CTRLDOWN}c{CTRLUP}
          Sleep, 200
          Location := clipboard
          Location:=Enc_Underscore(Location)
          If Location contains %EchoOffice%
               SendInput {tab}%Action%
          Else
          {
               Sleep, 1000
               Pg := Pg + 1
               If StrLen(Pg) < 2
                    Pg := "0" . Pg
               SendInput {HOME}{SHIFTDOWN}{TAB}{SHIFTUP}%Pg%{enter}
               Sleep, 1000
               Continue
          }
     }
     Break
}
Sleep, 100
Return


Enc_Underscore(String)
{
     StringCaseSenseSetting:=A_StringCaseSense
     StringCaseSense Off
          Loop, Parse, String
          {
               Test := asc(A_LoopField)
               If (Test = 95)
               {
                    Replace := " "
                    TranslatedString := TranslatedString . Replace
               }
               Else
               {
                    TranslatedString := TranslatedString . A_Loopfield
                    Ind = 1
               }
          }
     StringCaseSense %StringCaseSenseSetting%
     Return TranslatedString
}


So, thanks again HugoV (and infogultch for posting where I could find it)for this wonderful piece of code I was able to not only use several times, but also was able to learn a lot about AHK programming!!! :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2008, 10:29 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
No problem, that is what the forum is for, glad you learned something.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bon and 14 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group