MDK
MDK

Reputation: 57

Convert native byte...?

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

Answers (2)

Jaziri Rami
Jaziri Rami

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

SingerOfTheFall
SingerOfTheFall

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

Related Questions