How can I convert TBytes to RawByteString?

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.


You could consider using move (untested)

function Convert(const Bytes: TBytes): RawByteString; 
begin
  SetLength(Result, Length(Bytes));
  Move(Bytes[0], Result[1], Length(Bytes))  
end;

And use "const" for the parameter so the array is not copied twice.


The easiest way to convert bytes to a RawByteString is to use SetString().

Also, to avoid data loss if the RawByteString is ever assigned to other String types, you should assign a codepage to the RawByteString so that its character data gets converted correctly to the charset of the receiving String:

function Convert(const Bytes: TBytes): RawByteString; 
begin
  SetString(Result, PAnsiChar(PByte(Bytes))^, Length(Bytes));
  SetCodePage(Result, ..., False);
end;