user560913
user560913

Reputation: 315

How to Convert from unsigned char* to array<unsigned char>^?

How do I convert an array of unsigned chars to

 array<unsigned char>^ ?

Thanks in advance!

Upvotes: 1

Views: 10463

Answers (1)

David Yaw
David Yaw

Reputation: 27864

Just create a managed array, and copy the data. Simple.

array<Byte>^ MakeManagedArray(unsigned char* input, int len)
{
    array<Byte>^ result = gcnew array<Byte>(len);
    for(int i = 0; i < len; i++)
    {
        result[i] = input[i];
    }
    return result;
}

Yes, I'm sure there's a way to use the Marshal class to do the copy for you, or to get a pointer to the managed array you can pass to memcpy, but this works, and it doesn't require research on MSDN to verify that it's correct.

Upvotes: 7

Related Questions