6

Is it possible to convert byte* to Array^ in C++/CX?

Currently I accomplish this by copying each value, which I know is not space/performance efficient.

My current implementation is :

Array<byte>^ arr = ref new Array<byte>(byteCount);
for (int i = 0; i < byteCount; i++)
{
    arr[i] = *(bytes + i);
}
3
  • 1
    Use ArrayReference<> instead. Commented Jun 1, 2014 at 17:05
  • @HansPassant MSDN says "By using ArrayReference to fill a C-style array, you avoid the extra copy operation that would be involved in copying first to a Platform::Array variable, and then into the C-style array". So I guess ArrayReference is used for copying to a C style array when my use case is to copy from a C style array to Platform::Array class. Commented Jun 2, 2014 at 6:47
  • Do note that passing an Array across the ABI will incur a copy anyway. To avoid copies, use IBuffer (although avoiding the copy may be a little tricky if one of the parties is .NET.. it's certainly possible). Commented Jul 23, 2014 at 11:28

3 Answers 3

7

There's an array constructor for that (MSDN): Array(T* data, unsigned int size);

So in your case, simply do: Array<byte>^ arr = ref new Array<byte>(bytes, byteCount);

This is a great article on C++/CX and WinRT array patterns.

Sign up to request clarification or add additional context in comments.

Comments

1

If you care about performance use Platform::ArrayReference instead Platform::Array

And if you will need Platform::Array you can just cast like this

uint8_t *data;
size_t data_size;
...

auto arrayRef = new ArrayReference<uint8_t>(data, data_size);
Array^ array = reinterpret_cast<Array<uint8_t>^>(arrayRef)

Comments

0

Yes you can also use

Array<byte>^ t = ref new Platform::Array<byte>(byteReaded);
CopyMemory(t->Data, buff, byteReaded);

for copy or for access using

t->Data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.