How do I convert a pointer to a byte array?
The first byte indicates the number of bytes to follow.
The safe thing to do is to make a copy of the data pointed to.
If you have a byte* then you can of course just write the code yourself:
byte* source = whatever;
int size = source[0]; // first byte is size;
byte[] target = new byte[size];
for (int i = 0; i < size; ++i)
target[i] = source[i+1];
Easy peasy.
If instead of a byte* you have an IntPtr then you can use this helpful method:
http://msdn.microsoft.com/en-us/library/ms146631.aspx
There are lots of helpful methods on the Marshal class.
Well, the byte*
isn't the array object. You can get the address of the data (using fixed
etc), but an arbitrary byte*
does not have to be the start of the data - it could be at offset 17, for example.
So I would recommend either:
byte[]
around insteadbyte[]
and copy over the data you wantIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With