cropping a byte array?

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.
 
M

Mattias Sjögren

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
 
W

Will

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++.
 
M

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top