Sample code for 30+ languages & platforms
Lazarus Pascal

Append Encoded Binary Data to StringBuilder

Demonstrates how to append encoded binary data to the contenets of a StringBuilder.

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,
  Chilkat.StringBuilder;

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

procedure RunDemo;
var
  success: Boolean;
  bd: TBinData;
  sb: TStringBuilder;

begin
  success := False;

  bd := TBinData.Create;

  success := bd.LoadFile('qa_data/jpg/starfish.jpg');
  if (success = False) then
    begin
      WriteLn('Failed to load file.');
      Exit;
    end;

  //  For example, let's say we want construct simple JSON containing the base64 representation of the above JPG file.
  sb := TStringBuilder.Create;
  sb.Append('{ "jpg": "');
  //  GetEncodedSb appends the enocded representation of the binary data to the StringBuiler passed in the 2nd arg.
  bd.GetEncodedSb('base64',sb);
  sb.Append('" }');

  WriteLn(sb.GetAsString());

  //  Output looks like this:
  //   { "jpg": "/9j/4AAQSkZJRgABAg...rcQ+vo//2Q==" }


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