BASE64 Encode and Decode is not working
android, base64, delphi, delphi-xe5
Solution
Officially, Delphi DOES NOT support `AnsiString` and `(P)AnsiChar` on mobile platforms. Unofficially, the support code for them is still present in the compiler and RTL, it is just hidden so you cannot access it anymore. There is a third-party patch available that re-enables access.
When encoding/decoding a string, you have to take character encoding into account. Base64 encodes bytes, not characters. You have to convert a string to a byte sequence before then Base64 encoding the bytes, and then Base64 decode the byte sequence before then converting it back to a string.
The `TIdEncoderMIME.EncodeString()` and `TIdDecoderMIME.DecodeString()` methods have an optional `TIdTextEncoding` or `IIdTextEncoding` parameter (depending on your version of Indy) for that string<->bytes conversion. If you do not specify a text encoding, Indy will use its default text encoding, which is 7bit ASCII by default (configurable via the `IdGlobal.GIdDefaultTextEncoding` variable).
For example:
uses
..., IdGlobal, IdCoderMIME;
function Encode64(const S: string: const ByteEncoding: IIdTextEncoding = nil): string;
begin
Result := TIdEncoderMIME.EncodeString(S, ByteEncoding);
end;
function Decode64(const S: string: const ByteEncoding: IIdTextEncoding = nil): string;
begin
Result := TIdDecoderMIME.DecodeString(S, ByteEncoding);
end;
uses
..., IdGlobal;
var
s, base64: string;
begin
s := '€$';
base64 := Encode64(s, IndyTextEncoding_UTF8);
s := Decode64(base64, IndyTextEncoding_UTF8);
end;
uses
..., IdGlobal;
var
s, base64: string;
enc: IIdTextEncoding;
begin
enc := IndyTextEncoding(28592); // ISO-8859-2
s := '€$';
base64 := Encode64(s, enc);
s := Decode64(base64, enc);
end;
uses
..., IdGlobal, IdGlobalProtocols;
var
s, base64: string;
enc: IIdTextEncoding;
begin
enc := CharsetToEncoding('ISO-8859-2');
s := '€$';
base64 := Encode64(s, enc);
s := Decode64(base64, enc);
Problem
I am working on android applicatin in Delphi XE5 and I need to BASE64 encode and decode some strings. This function is working fine for english characters, but what I want to encode €, $ or any special iso8859-2 characters encoding doesn't work. Any idea how to fix it? I found BASE64 unit http://www.delphipraxis.net/991-base64-mime-en-decoding.html But does FireMonkey support AnsiString and PAnsiChar type and what unit to include to use this type? My code ``` uses IdCoderMIME; ... function Encode64(S: string): string; var IdEncoderMIME: TIdEncoderMIME; begin try IdEncoderMIME := TIdEncoderMIME.Create(nil); Result := IdEncoderMIME.EncodeString(S); finally IdEncoderMIME.Free; end; end; function Decode64(S: string): string; var IdDecoderMIME: TIdDecoderMIME; var IdDecoderMIME: TIdDecoderMIME; begin try IdDecoderMIME := TIdDecoderMIME.Create(nil); Result := IdDecoderMIME.DecodeString(S); finally IdDecoderMIME.Free; end; end; ```