On Mon, 15 Aug 2011 13:16:54 -0400, mimocrocodil <[email protected]> wrote:

I obtain immutable(byte)* and int where contained length of bytes block from C library.

Can I convert this into byte[] without explict copying etc.

Something like:

byte* p; // bytes
int size; // size of bytes block

byte[] b;
b.length = size;
b.ptr = p;

// now b contains bytes from library

It's even easier:

auto b = p[0..size]; // b is of type byte[]

I.e. you can use a slice operation on a pointer to create a correctly-typed slice.

Keep in mind, the size is the number of *elements* for the slice, not the number of *bytes*. In your case it happens to be identical, but for larger element types it would be different.

-Steve

Reply via email to