Last active
February 22, 2023 18:31
-
-
Save tmplinshi/3776c215152436c6f1d722e0151d2fcb to your computer and use it in GitHub Desktop.
Base64 Encode/Decode support UTF-16-LE encoding. Ported from VBS code https://stackoverflow.com/a/40118072/1631371.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b64_encode(p*) { | |
return b64.encode(p*) | |
} | |
b64_decode(p*) { | |
return b64.decode(p*) | |
} | |
class b64 | |
{ | |
encode(ByRef sText, bIsUtf16 := false) | |
{ | |
ele := ComObjCreate("Msxml2.DOMDocument").CreateElement("aux") | |
ele.DataType := "bin.base64" | |
ele.NodeTypedValue := bIsUtf16 ? this.strToBytes(sText, "utf-16le", 2) | |
: this.strToBytes(sText, "utf-8", 3) | |
return ele.Text | |
} | |
encode_fromfile(filename, ByRef result) | |
{ | |
ele := ComObjCreate("Msxml2.DOMDocument").CreateElement("aux") | |
ele.DataType := "bin.base64" | |
ele.NodeTypedValue := this.fileToBytes(filename) | |
result := StrReplace(ele.Text, "`n") | |
} | |
fileToBytes(FileName) | |
{ | |
oADO := ComObjCreate("ADODB.Stream") | |
oADO.Type := 1 ; adTypeBinary | |
oADO.Open | |
oADO.LoadFromFile(FileName) | |
return oADO.Read, oADO.Close | |
} | |
decode(ByRef sBase64EncodedText, bIsUtf16 := false) | |
{ | |
ele := ComObjCreate("Msxml2.DOMDocument").CreateElement("aux") | |
ele.DataType := "bin.base64" | |
ele.Text := sBase64EncodedText | |
return this.bytesToStr(ele.NodeTypedValue, bIsUtf16 ? "utf-16le" : "utf-8") | |
} | |
strToBytes(ByRef sText, sEncoding, iBomByteCount) | |
{ | |
oADO := ComObjCreate("ADODB.Stream") | |
oADO.Type := 2 ; adTypeText | |
oADO.Mode := 3 ; adModeReadWrite | |
oADO.Open | |
oADO.Charset := sEncoding | |
oADO.WriteText(sText) | |
oADO.Position := 0 | |
oADO.Type := 1 ; adTypeBinary | |
oADO.Position := iBomByteCount ; skip the BOM | |
return oADO.Read, oADO.Close | |
} | |
bytesToStr(byteArray, sTextEncoding) | |
{ | |
oADO := ComObjCreate("ADODB.Stream") | |
oADO.Type := 1 ; adTypeBinary | |
oADO.Mode := 3 ; adModeReadWrite | |
oADO.Open | |
oADO.Write(byteArray) | |
oADO.Position := 0 | |
oADO.Type := 2 ; adTypeText | |
oADO.Charset := sTextEncoding | |
return oADO.ReadText, oADO.Close | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This saved me so much time, thanks :D