Question:
I use Ward’s AutoIt Machine Code Algorithm Collection to get base64 encoding of a string in AutoIt:
1 2 3 4 5 |
#Include "Base64.au3" Dim $Encode = _Base64Encode("ps") MsgBox(0, 'Base64 Encode Data', $Encode) |
The result:
1 2 |
cHM= |
PowerShell code to get the base64 encoding of the same string “ps”:
1 2 3 4 5 |
$commands = 'ps' $bytes = [System.Text.Encoding]::Unicode.GetBytes($commands) $encodedString = [Convert]::ToBase64String($bytes) $encodedString |
What I got is:
1 2 |
cABzAA== |
The result from PowerShell is what I want. How to get the same result using AutoIt? I guess this is a character encoding issue.
Answer:
When I ran this script:
1 2 3 4 5 |
#Include "Base64.au3" $Decode = _Base64Decode("cABzAA==") ConsoleWrite($Decode & @CRLF) |
I get the result: 0x70007300. Basically, this means there is a ’70’ character (p), a ’00’ character (nul), a ’73’ character (s), ’00’ character. You can easily recreate this behavior in AutoIt with a function like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#Include "Base64.au3" Dim $Encode = _Base64WEncode("ps") ConsoleWrite($Encode & @CRLF) Func _Base64WEncode($string) Local $result = "" Local $arr = StringSplit($string, "") For $i = 1 To UBound($arr) - 1 $result &= $arr[$i] & Chr(0) Next $result = _Base64Encode($result) Return $result EndFunc |
The result is: cABzAA==
Somewhat hack-ish, but I’d say it is preferred over full Unicode encoding if that’s not what you will ever need.