byte* and int length -> byte[]
Steven Schveighoffer
schveiguy at yahoo.com
Mon Aug 15 10:21:58 PDT 2011
On Mon, 15 Aug 2011 13:16:54 -0400, mimocrocodil <4denizzz at gmail.com>
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
More information about the Digitalmars-d-learn
mailing list