Reputation: 57
How can I convert a native byte (unsigned char) type variable to a managed byte array which is array^ in C++?
byte sData[255]; // convert to System::Byte array or copy content to byteArray?? How?
array<System::Byte>^ byteArray;
Thankyou for your assiastance...
Upvotes: 3
Views: 1819
Reputation: 317
int size = Mat.total() * Mat.elemSize();
byte * bytes = new byte[size];
std::memcpy(bytes, Mat.data, size * sizeof(byte));
// convert native byte array to managed Byte array
array<Byte> ^byteArray = gcnew array<Byte>(size + 2);
Marshal::Copy((IntPtr)bytes, byteArray, 0, size);
Upvotes: 0
Reputation: 29966
You can do it this way:
char buf[] = "Native String";
int len = strlen(buf);
array<Byte> ^byteArray = gcnew array<Byte>(len + 2);
Marshal::Copy( (IntPtr)buf, byteArray, 0, len );
You can find some more info in this MSDN article.
Upvotes: 3