
Conversion of native char* to managed array
Hi Brandon, Marat,
Quote:
> > What would be the most efficient way of converting a native character
> > pointer or character array to a managed array, without having to
> > allocate a temporary managed array and manually copying into it.
> You have to create a managed array, and arrays are passed by __gc
pointers.
> Thus, you would never be able to create a "temporary" managed array.
> So, the most efficient way is also the simplest way. Just create a managed
> array of the correct length and then copy each value directly into the
array
> with a loop.
Hum... I think this is either simpler, or more complex than just that,
depending on how you look at it.
marat's asking about copying a char* to a character managed array. hence,
that likely means doing an encoding conversion, since that would imply
char* -> System::Char[]. In that case, I'd have to wonder if just creating a
temporary System::String object and then calling String::ToCharArray(),
wouldn't just be simpler.
If, on the oither hand, what he really wants is a char*->System::Byte[]
conversion, then the Copy() method of the Marshal class would probably be
faster than doing the loop yourself :)
--
Tomas Restrepo