13 Votes

Lazarus: Load File as Byte Array and save Byte Array as File

Tutorial by Delphian | Last update on 2021-04-02 | Created on 2018-01-01

In this tutorial I want to show you how to read a file byte by byte into an array and how to write a byte array into a file again.

First, let's define a byte array:

type
  TByteArr = array of Byte;

We can now fill it with some individual bytes:

var
  BA: TByteArr;
begin
  SetLength(BA, 3);
  BA[0] := 65;
  BA[1] := $41;
  BA[2] := %1000001;

Thereby, it does not matter if we specify the bytes in decimal, hexadecimal or binary notation.

65 is the ASCII code for "A". In hexadecimal notation, the number 65 corresponds to 41 and in binary notation to 1000001.

In the example, we set all three bytes to the same value. Decimal values can be written as simple integer numbers in the code, hexadecimal numbers are denoted by $ and binary numbers by %.

Save Byte Array

Now let's look at how we can save our byte array to a file. For this we are using the following function:

procedure SaveByteArray(AByteArray: TByteArr; const AFileName: string);
var
  Stream: TStream;
begin

  if FileExistsUTF8(AFileName) then DeleteFileUTF8(AFileName);

  Stream := TFileStreamUTF8.Create(AFileName, fmCreate);
  try
     Stream.WriteBuffer(Pointer(AByteArray)^, Length(AByteArray));
  finally
     Stream.Free;
  end;

end;  

A usage example may look like this:

var
  BA: TByteArr;
begin
  SetLength(BA, 1);
  BA[0] := 65;
  SaveByteArray(BA, 'file.dat');

As the first parameter we pass our byte array, as second parameter the desired file name.

Load Byte Array

To load a file from the hard disk into a byte array, the following function is useful:

function LoadByteArray(const AFileName: string): TByteArr;
var
  Stream: TStream;
  DataLeft: Integer;
begin
  SetLength(result, 0);

  if not FileExistsUTF8(AFileName) then exit;

  Stream := TFileStreamUTF8.Create(AFileName, fmOpenRead or fmShareDenyWrite);
  try
     stream.Position:=0;
     DataLeft := Stream.Size;
     SetLength(result, DataLeft div SizeOf(Byte));
     Stream.Read(PByte(result)^, DataLeft);
  finally
     Stream.Free;
  end;

end; 

A call to the function may look like this:

var
  BA: TByteArr;
begin
  BA := LoadByteArray('file.dat');

We only pass a file name as parameter and get back the byte array.

Visualize Byte Array

When working with byte arrays, sooner or later the question of displaying the content of the byte array comes up. It makes sense to output the byte arrays in hexadecimal form as a string. For example, an output of the above-mentioned byte array would look like this: "41 41 41".

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Save Binary Data as String

Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.