Convert BYTE* to Array<byte>^
Asked Answered
G

3

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);
}
Goldschmidt answered 1/6, 2014 at 12:24 Comment(3)
Use ArrayReference<> instead.Galloromance
@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.Goldschmidt
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).Pontias
M
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.

Malaise answered 4/6, 2014 at 0:40 Comment(0)
V
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)
Veridical answered 24/9, 2016 at 13:29 Comment(0)
P
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
Preordain answered 6/5, 2015 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.