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
This is what we have so far:
- XML encode/decode by infogulch
- URI encode/decode by Titan
- HEX encode/decode by Lazlo
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, ", ", All ;required predefined character entities &"<'>
StringReplace, str, str, ', ', All
StringReplace, str, str, <, <, All
StringReplace, str, str, >, >, All
StringReplace, str, str, &, &, 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, &, &, All ;do first so it doesn't re-encode already encoded characters
StringReplace, str, str, ", ", All ;required predefined character entities &"<'>
StringReplace, str, str, ', ', All
StringReplace, str, str, <, <, All
StringReplace, str, str, >, >, 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:
- English/Albed Translator by HugoV (for Final Fantasy)
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.
