My variant of code works fine with
Using a
string to hold non-textual binary data is a bad idea.
a pointer to the body of the string (offset 0).
The leading structure of a string does not
contain a pointer to the character data. The character data
immediately follows the structure in memory. Two different things. Offset 0 into the string is the same memory address as the 1st character of the string.
So body of the string has offset +1.
The leading structure has nothing to do with why the offset of the character data starts at +1. It is because of backwards compatibility with
ShortString, whose length is stored in the character at offset 0 and the 1st character starts at offset 1. And so,
AnsiString (and
UnicodeString) access their character data beginning at offset 1 too, making it easier for people to migrate older code to newer string types.