Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Base64 Encode a File

_LANGUAGE_ to Base64 encode the contents of a file.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.FileAccess;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  fac: TFileAccess;
  strBase64: string;

begin
  success := False;

  //  Get the contents of a file into a base64 encoded string:
  fac := TFileAccess.Create;
  strBase64 := fac.ReadBinaryToEncoded('c:/data/something.pdf','base64');
  if (fac.LastMethodSuccess <> True) then
    begin
      WriteLn(fac.LastErrorText);
      Exit;
    end;

  //  Now write the string to a file:
  success := fac.WriteEntireTextFile('c:/data/something_pdf_base64.txt',strBase64,'us-ascii',False);
  if (success <> True) then
    begin
      WriteLn(fac.LastErrorText);
      Exit;
    end;

  WriteLn('Success!');


  fac.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.