Hidden Gems: Base64 Encoding and Decoding in Delphi

One of the big probs with Delphi these days is the sheer amount of functionality that is already included in the package. Just today I was pondering how to encode into or decode from Base64 with Delphi. 

As it turns out, Embarcadero included the functionality in the SOAP source files that can be found in the “soap” directory.

One unit in particular, called Soap.EncdDecd , contains the functions that I was looking for. However, the comments in the source file pointed me yet in another direction:

procedure EncodeStream(Input, Output: TStream); inline; // deprecated 'Use TNetEncoding.Base64.Encode';
procedure DecodeStream(Input, Output: TStream); inline; // deprecated 'Use TNetEncoding.Base64.Decode';
function  EncodeString(const Input: string): string; inline; // deprecated 'Use TNetEncoding.Base64.Encode';
function  DecodeString(const Input: string): string; inline; // deprecated 'Use TNetEncoding.Base64.Decode';

You find the TNetCoding class in the unit System.NetEncoding :

TNetEncoding = class
  // ...
  public
    function Decode(const Input, Output: TStream): Integer; overload;
    function Decode(const Input: array of Byte): TBytes; overload;
    function Decode(const Input: string): string; overload;
    function Encode(const Input, Output: TStream): Integer; overload;
    function Encode(const Input: array of Byte): TBytes; overload;
    function Encode(const Input: string): string; overload;
    function DecodeStringToBytes(const Input: string): TBytes;
    function EncodeBytesToString(const Input: array of Byte): string; overload;
    function EncodeBytesToString(const Input: Pointer; Size: Integer): string; overload;
    class property Base64: TNetEncoding read GetBase64Encoding;
    class property HTML: TNetEncoding read GetHTMLEncoding;
    class property URL: TURLEncoding read GetURLEncoding;
  end;

The class methods Base64, HTML and URL allow us to encode strings, byte arrays and streams. To Base64 encode a string, we simply need to state:

TNetEncoding.Base64.Encode(myString);

Overloading makes it very easy as the call is the very same for steams and byte arrays.

A simple demo with 3 TEdit  components shows the functionality. The text entered in the first edit is displayed encoded in the second. The third edit displays the text from the second after decoding.

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1Change(Sender: TObject);
begin
  Edit2.Text := TNetEncoding.Base64.Encode(Edit1.Text);
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  Edit3.Text := TNetEncoding.Base64.Decode( Edit2.Text );
end;

Considering that we needed 3rd party component libraries for these things before, shows how significantly Delphi has grown. The biggest problem nowadays is to find the right class for the job and keep track of the new innovations available to us.

base64demo

 

Tags: , , , , ,

Partnerships




Top