Sample code for 30+ languages & platforms
Lazarus Pascal

Convert any File to Base64 (and back)

Demonstrates how to get the contents of any file as a base64 string, and then write it back.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.BinData;

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

procedure RunDemo;
var
  success: Boolean;
  bd: TBinData;
  b64Str: string;
  bd2: TBinData;

begin
  success := False;

  bd := TBinData.Create;

  //  This example will load a PDF and return it as a base64 string.
  success := bd.LoadFile('qa_data/pdf/helloWorld.pdf');
  if (success <> True) then
    begin
      WriteLn('Failed to load file.');
      Exit;
    end;

  b64Str := bd.GetEncoded('base64');
  WriteLn(b64Str);

  //  Now write the base64 string back to the binary PDF file:
  bd2 := TBinData.Create;
  success := bd2.AppendEncoded(b64Str,'base64');
  success := bd2.WriteFile('qa_output/helloWorld.pdf');


  bd.Free;
  bd2.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.