Skip to content

Base64 Encoding and Decoding

Base64 has become a popular means to encode binary information. It is very easy to create a Base64 string from binary data and reconstruct binary data from a Base64 string. As a matter of fact, it is included in the base version of Delphi now and no 3rd party libraries are needed.

Do not use this approach with TMS WEB Core

Please refer to a separate post specifically created for TMS WEB Core. This post uses units which are not available on the TMS WEB Core platform.

One of the big issues 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 three 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.

Video showing example application to convert string to Base64.