Board index » cppbuilder » Byte array in AnsiString

Byte array in AnsiString


2004-04-29 05:39:10 PM
cppbuilder101
Hello,
Is it possible to get a byte-array into a Ansi String object (I need to
"cast" a byte-array to a AnsiString object) ? I tried to write directly
to the *data pointer from AnsiString, but it is a private variable of
the class... If there is a solution to construct a AnsiString object
from a byte-array, what about the copy and assignment constructors in
AnsiString, would they contain the complete byte-array, or would they
truncate it at the first 0 byte (string termination) ?
Thanks in advance.
Regards,
Danny
 
 

Re:Byte array in AnsiString

Danny wrote:
Quote
Is it possible to get a byte-array into a Ansi String object (I need to
"cast" a byte-array to a AnsiString object) ? I tried to write directly
to the *data pointer from AnsiString, but it is a private variable of
the class... If there is a solution to construct a AnsiString object
from a byte-array, what about the copy and assignment constructors in
AnsiString, would they contain the complete byte-array, or would they
truncate it at the first 0 byte (string termination) ?
Did you try something like this already ?
BYTE mybytes [] = "aaaaaaaaaaaaaaaa";
AnsiString MyBytes = (char*)mybytes;
ShowMessage ( MyBytes );
Or does it change the BYTES ?
Hans.
 

Re:Byte array in AnsiString

Danny <nospam>wrote in message news: XXXX@XXXXX.COM ...
Quote
AnsiString, would they contain the complete byte-array, or would
they
truncate it at the first 0 byte (string termination) ?
This is the key point in your post; Many AnsiString methods or VCL
techniques (besides your assignment problem) don't work properly with
embedded zeros, because of the assumption that a "real" char string is
involved. Also consider that the data area is dynamically allocated,
so you'd need to assign a string of the correct length before
accessing each position with a for loop using the [] AnsiString
operator. (That's one reason why you can't assign a string to the
pointer!) I recommend another idea if there are embedded zeros.
 

{smallsort}

Re:Byte array in AnsiString

"Danny" < XXXX@XXXXX.COM >wrote in message
Quote
Is it possible to get a byte-array into a Ansi String object
Yes.
Quote
I need to "cast" a byte-array to a AnsiString object
Why?
Quote
I tried to write directly to the *data pointer from AnsiString,
but it is a private variable of the class...
Use the data() method to get at the pointer. Make sure that you sized the
AnsiString to the size of the array first, otherwise the pointer will be
NULL.
Quote
If there is a solution to construct a AnsiString object from a byte-array
Simply pass the array to AnsiString's constructor, and also specify the
length of the array:
AnsiString s((char*)myarray, myarraysize);
Quote
what about the copy and assignment constructors in AnsiString,
would they contain the complete byte-array, or would they truncate
it at the first 0 byte (string termination) ?
The assignment operator will truncate if you try to assign the array
directly. You will have to construct a temporary AnsiString and then assign
it instead:
s = AnsiString((char*)myarray, myarraysize);
Gambit
 

Re:Byte array in AnsiString

"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
Did you try something like this already ?
That will still truncate on the first embedded '\0' byte in the array. See
my other reply.
Quote
AnsiString MyBytes = (char*)mybytes;
Gambit