Reputation: 13345
I have an AnsiString
and I need to convert it in the most efficient way to a TBytes
. How can I do that ?
Upvotes: 4
Views: 7375
Reputation: 4730
The function BytesOf
converts an AnsiString to TBytes.
var
A: AnsiString;
B: TBytes;
begin
A := 'Test';
B := BytesOf(A);
// convert it back
SetString(A, PAnsiChar(B), Length(B));
end;
Upvotes: 11
Reputation: 612884
Assuming you want to retain the same encoding you can do this
SetLength(bytes, Length(ansiStr));
Move(Pointer(ansiStr)^, Pointer(bytes)^, Length(ansiStr));
In reverse it goes
SetLength(ansiStr, Length(bytes));
Move(Pointer(bytes)^, Pointer(ansiStr)^, Length(bytes));
Upvotes: 7