Conversion of native char* to managed array 
Author Message
 Conversion of native char* to managed array

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.

ex:

// unmanaged c++ code
char buff[N];
someObject.bytes(buff, size);  
     ....
// managed c++ code
int n = socket->SendTo(ptr, ep);   // 1st argument expects a managed
byte array

Declaring a new char b __gc[] an memcpy() didn't work, but looping and
copying each element works...

MR



Sat, 29 Oct 2005 01:47:32 GMT  
 Conversion of native char* to managed array

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.

Cheerio!

--
Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.



Sat, 29 Oct 2005 02:23:06 GMT  
 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



Sat, 29 Oct 2005 07:41:31 GMT  
 Conversion of native char* to managed array

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.

Is it possible to do a memcpy instead of having a loop for this?
Quote:
> Cheerio!



Sat, 29 Oct 2005 09:23:36 GMT  
 Conversion of native char* to managed array

Quote:
> Hi Brandon, Marat,

> > > 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 :)

Thanks - that works and is more eye-pleasing than a loop, and
hopefully more efficient.


Sun, 30 Oct 2005 03:19:37 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Managed array of managed arrays of char

2. Conversion of managed and unmanaged arrays

3. char arrays/conversion

4. substring of char array 2 LONG conversion....

5. Differences between char array[SIZE] and char *array

6. no Intellisense with mixed Managed/Native C++

7. VC++ managed code compiles to native?

8. can't step through mixed managed/native code

9. Using native buffers in managed code?

10. Using native buffers in managed code?

11. Converting char array of literals chars to escape chars

12. A char pointer (char *) vs. char array question

 

 
Powered by phpBB® Forum Software