Dels
Dels

Reputation: 2425

convert hex formatted data into string

I have some data formatted in hex (stored in byte) that i need to convert to string, the data should be like this

13 61 23 45 67 8F FF

and expected output

13612345678FFF

i know Chr() or IntToStr() function will not work since this data not actual hex code but string/number formatted in hex, so anyone has some suggestion for this?

Upvotes: 2

Views: 4762

Answers (2)

Thorsten Engler
Thorsten Engler

Reputation: 2371

This is for converting a dynamic array of bytes to a hex string:

function BytesToHex(aSource: TBytes): string;
begin
  SetLength(Result, Length(aSource) * 2);
  if Length(aSource) > 0 then
    BinToHex(aSource[0], PChar(Result), Length(aSource));
end;

If your source bytes are not in a dynamic array, you'll have to adjust the code slightly, but it should give you a general idea of how to do it.

Upvotes: 4

Wouter van Nifterick
Wouter van Nifterick

Reputation: 24116

With the example that you've provided, why don't you just remove the spaces?

s := '13 61 23 45 67 8F FF';

stripped := StrUtils.ReplaceStr(s,' ','');

if your hex string is not too long, you can get it as a number like this:

MyInt64 := StrToInt64('$' + stripped);

Check out HexToBin() if you want to get your hex string as an array of bytes.

Upvotes: 1

Related Questions