Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert TBytes to RawByteString?

What is the best way to convert an array of bytes declared as TBytes to a RawByteString in Delphi 2009? This code actually works, maybe there is a faster way (without loop):

   function Convert(Bytes: TBytes): RawByteString; 
   var
     I: Integer;
   begin
     SetLength(Result, Length(Bytes));
     for I := 0 to ABytes - 1 do
       Result[I + 1] := AnsiChar(Bytes[I]);
   end;
like image 626
mjn Avatar asked Apr 21 '09 15:04

mjn


1 Answers

The best way is this:

function Convert(const Bytes: TBytes): RawByteString; inline;
begin
  SetString(Result, PAnsiChar(pointer(Bytes)), length(Bytes));
end;

And don't forget to use const for Bytes parameters, for somewhat faster generated code.

like image 66
A.Bouchez Avatar answered Sep 25 '22 21:09

A.Bouchez