mark_c wrote:in this small example, I don't understand why AnsiString has problems with the null character and the string is truncated?
The data is truncated because you are invoking operator+= using a char[] array as input.
When referred to by its name alone, a char[] array *decays* into a char* pointer to its first element. The operator takes an AnsiString as input, not a char* pointer. However, AnsiString has a constructor that takes a char* as input, and treats it as a null-terminated string, stopping on the first null char it encounters in memory. Which in your case is at buf[4].
To do what you are attempting, explicitly construct a temp AnsiString containing the full array data, and then pass that temp as the input value for the operator. AnsiString has another constructor that takes both a char* pointer and a size as input, copying the char data as-is up to the specified size, eg:
Code: Select all
AnsiString MyBuffer;
const char buf[] = "\xff\xd8\xff\xe0\x00\xff\xd7\xff";
MyBuffer += AnsiString(buf, 8);
Then, the code can be simplified by omitting the operator altogether:
Code: Select all
const char buf[] = "\xff\xd8\xff\xe0\x00\xff\xd7\xff";
AnsiString MyBuffer(buf, 8);
mark_c wrote:I want to store a jpeg image in an AnsiString buffer for an experiment
What kind of experiment? You really should not be storing binary data in an AnsiString to begin with. Too many ways the data could get corrupted if the AnsiString is misused. There are better containers designed for binary data, such as TBytes and std::vector.