11 Vote

Delphi/Lazarus: Show Byte Array as String of HEX Values

Tip by Stefan Trost | Last update on 2023-02-18 | Created on 2018-01-01

When working with Byte Arrays sooner or later the question of visualization raises. Here it makes sense to output the individual bytes in hexadecimal form as a string in order to then display this string to the user, for example, in a memo field or via a message. In this tip, I want to show you a function that does just that.

In the example we are using the following definition of a byte array:

type
  TByteArr = array of Byte;

Function to convert Byte Arrays to Hex Strings

To visualize byte arrays of this type into a string, we use the following function. The function goes through the byte array passed as the first parameter byte by byte and converts each individual byte value to its corresponding hexadecimal value:

function ByteArrayToHexString(AByteArray: TByteArr; ASep: string = ''): string;
var
  i, k: integer;
begin
  result := '';

  if ASep = '' then begin
     for i := low(AByteArray) to high(AByteArray) do 
       result := result + IntToHex(AByteArray[i], 2);
  end else begin
     k := high(AByteArray);
     for i := low(AByteArray) to k do begin
        result := result + IntToHex(AByteArray[i], 2);
        if k <> i then result := result + ASep;
     end;
  end;
end; 

The function can either be called with just a byte array or optionally with a separator as the second parameter, which is written between the individual bytes for better clarity and readability. We have set this separator to an empty string by default in the function, so that when the function is called with only one parameter, no separator is written between the individual bytes.

Example Call of the Function

Let's look at an example of how we can use the presented function:

var
  BA: TByteArr;
  s: string;
begin
  SetLength(BA, 4);
  BA[0] := 65;
  BA[1] := $41;
  BA[2] := %1000001; // this only works in Lazarus
  BA[3] := ord('A');

  s := ByteArrayToHexString(BA);        // 41414141
  s := ByteArrayToHexString(BA, ' ');   // 41 41 41 41
end;

In this example, we first create a byte array that is 4 bytes long and then fill it with some values. Then we use our function to output these values.

Even if we have assigned "different" values to the individual array elements, each element of the array has the same value after filling. We just wrote the hexadecimal value "41" in different ways: once as a decimal value (65), once as a binary value (1000001) and once by calling the function ord('A'), which gives us the ASCII value of the letter "A", which is also 65 or #41.

Load and Save Byte Array

In this context, it is also interesting how to load or save a byte array from respectively to a file. You can find out more about this in the linked tutorial, which shows functions and examples for both cases.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

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.