Encode string to Base64 in Inno Setup (Unicode Version of Inno Setup)

天大地大妈咪最大 提交于 2019-12-06 14:45:57

Base64 encodes bytes, not characters (strings). That's also probably the reason, why the Encode64 implementation, that you have found, takes AnsiString. AnsiString is commonly (ab)used in Inno Setup Pascal Script as a dynamic array of bytes. While string is an array of characters.

If you want to encode a string, you first have to represent the string as an array of bytes (in a way the recipient of the Base64-encoded string expects it) and then you can use your Encode64 implementation.

In case you encode ASCII characters only, you can just blindly cast string to AnsiString. If you use non-ascii characters, you will probably want to convert your UnicodeString to bytes using some encoding, like UTF-8.

As for the resulting string, you can just safely cast it from AnsiString to string, as Base64 uses ASCII characters only (though, it also makes sense to change function signature to return string, as it indeed returns a character string, not byte array).

So for an ASCII input, this will do:

Base64 := string(Encode64(AnsiString(S)));

If you want to use a "standard" function, you can use CryptBinaryToString WinAPI function. Though that won't spare you from solving the above, as the function takes an array of bytes on input (as expected).


The above makes a difference only, if you use Unicode Inno Setup (what you correctly do). Had you used Ansi Inno Setup (what you should not), string is AnsiString.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!