Convert from byte * to byte []

How to convert a pointer to an array of bytes?

The first byte indicates the number of subsequent bytes.

+3
source share
2 answers

The safest thing is to make a copy of the data that it points to.

If you have a byte *, 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 you have IntPtr instead of byte *, you can use this useful method:

http://msdn.microsoft.com/en-us/library/ms146631.aspx

There are many useful methods in the marshal class.

+6
source

, byte* . ( fixed ..), byte* - , , 17.

:

  • byte[]
  • () byte[]
+3

Source: https://habr.com/ru/post/1754505/


All Articles