cropping a byte array?

  • Thread starter Thread starter Will
  • Start date Start date
W

Will

I have a byte array buffer that is of a static size.

byte[] buffer = new byte[512];

Data is being read into it using NetworkStream.Read

int countBytes = netStream.Read(buffer, 0, buffer.Length);

For my applicaion, I need to output exactly the number of bytes that were
read in, not the entire buffer. Basically, I need to know how to do
something like this (I know this won't work).

byte[] output = buffer.subString(0, countBytes);

How do I create a new byte array of the correct length?

Thanks in advance,
Will.
 
Will,
byte[] output = buffer.subString(0, countBytes);

How do I create a new byte array of the correct length?


byte[] output = new byte[countBytes];
Buffer.BlockCopy(buffer, 0, output, 0, countBytes);



Mattias
 
Thanks!

I wasn't aware C# allowed you to instantiate an array with a variable as the
array size.
I'm pretty sure...

int y = 50;
int[] x = new int[y];

....wouldn't compile in C++.
 
I'm pretty sure...

int y = 50;
int[] x = new int[y];

...wouldn't compile in C++.

If you correct the syntax it would certainly work

int x[] = new int[y];



Mattias
 
Back
Top