How to shift a byte-array?

P

Polaris

Hi C# Experts:

I have a byte-array:

byte [] byte_array = new byte[5];

byte_array[0] = 0;
byte_array[1] = 1;
byte_array[2] = 2;
byte_array[3] = 3;
byte_array[4] = 4;

Is there a way to shift the contents of the array to left by 2 byte? The end
result should be:

byte_array[0] = 2;
byte_array[1] = 3;
byte_array[2] = 4;
byte_array[3] = 0; // whatever is ok
byte_array[4] = 0; // whatever is ok

Thanks in advance!
Polaris
 
R

Roger Frost

Polaris said:
Hi C# Experts:

I have a byte-array:

byte [] byte_array = new byte[5];

byte_array[0] = 0;
byte_array[1] = 1;
byte_array[2] = 2;
byte_array[3] = 3;
byte_array[4] = 4;

Is there a way to shift the contents of the array to left by 2 byte? The
end
result should be:

byte_array[0] = 2;
byte_array[1] = 3;
byte_array[2] = 4;
byte_array[3] = 0; // whatever is ok
byte_array[4] = 0; // whatever is ok

Thanks in advance!
Polaris

I smell Homework... But ask your professor why "whatever" is OK...and don't
give up till he/she gives a good answer.
 
M

Marc Gravell

You can just use Array.CopyTo or Buffer.BlockCopy to copy 3 bytes
starting index 2 to index 0. If "whatever is OK", the simplest
approach is to not change last two bytes, so the final array would be
{2,3,4,3,4} - but you can also use Array.Clear to clear 2 bytes
starting index 3.

Marc
 

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