How to shift a byte-array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Polaris

    How to shift a byte-array?

    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

  • Roger Frost

    #2
    Re: How to shift a byte-array?

    "Polaris" <etpolaris@hotm ail.comwrote in message
    news:36C57BE2-6E1E-4B0A-AB03-6612517BABA0@mi crosoft.com...
    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.

    Comment

    • Marc Gravell

      #3
      Re: How to shift a byte-array?

      You can just use Array.CopyTo or Buffer.BlockCop y 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

      Comment

      Working...