Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Byte Array

Byte Array

Scheduled Pinned Locked Moved C#
data-structuresquestion
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    o m n i
    wrote on last edited by
    #1

    I have an array of bytes. I need to remove the first 16 bytes of the array, then append another byte array onto it. What do you suggest is the best way to do this?

    L D R A A 5 Replies Last reply
    0
    • O o m n i

      I have an array of bytes. I need to remove the first 16 bytes of the array, then append another byte array onto it. What do you suggest is the best way to do this?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, for small arrays, just do it; i.e. allocate a new array of correct length, and copy the data you want in it (see Array.Copy). for large arrays (hundreds of MB), don't do it; make sure you don't need it, i.e. either make your consumer smart enough to skip some, and combine two arrays, or teach your producer(s) to first disclose lengths so you can allocate the final array at once, then let the producer(s) fill their allotted space. FYI: most .NET methods that take an array also have an overload that takes (array, startindex, length) in an attempt to alleviate such problems. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Happy New Year to all.
      We hope 2010 soon brings us automatic PRE tags!
      Until then, please insert them manually.


      1 Reply Last reply
      0
      • O o m n i

        I have an array of bytes. I need to remove the first 16 bytes of the array, then append another byte array onto it. What do you suggest is the best way to do this?

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        I addition to Luc's advice, have a look at the System.Buffer[^] class, specifically it's BlockCopy[^] method.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        1 Reply Last reply
        0
        • O o m n i

          I have an array of bytes. I need to remove the first 16 bytes of the array, then append another byte array onto it. What do you suggest is the best way to do this?

          R Offline
          R Offline
          Rick Shaub
          wrote on last edited by
          #4

          I rarely use Arrays in a situation like this. I prefer using the generic List[^] class. Using a list for your application would look like this:

          Using System.Collections.Generic;
          [...]

          List<Byte>myList = new List<Byte>();

          //Add range from some byte array:
          myList.AddRange(someByteArray);

          //Remove first 16 bytes:
          myList.RemoveRange(0,16);

          //Append some other array:
          myList.AddRange(someOtherByteArray);

          //Finally, get an array representation of this list:
          Byte[] retVal = myList.ToArray();

          return retVal;

          1 Reply Last reply
          0
          • O o m n i

            I have an array of bytes. I need to remove the first 16 bytes of the array, then append another byte array onto it. What do you suggest is the best way to do this?

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #5

            If you really must use an array, then the others have already given you perfectly reasonable options. However, you could also create a new data structure that is like a list but that you can remove items from both sides without a speed penalty. A list works by internally storing an array and removing/adding elements near the end of the array as needed. If you remove items from the beginning of a list, all the rest of the items will have to be shifted to the beginning to get rid of those empty elements. However, your class (DoubleSidedList?) could just keep the values near the center of the array. That way, you could trim or add to either side without worrying about speed problems when expanding or contracting either side of the collection. Naturally, this would incur a small performance penalty due to wrapping an array in a collection type.

            [Forum Guidelines]

            1 Reply Last reply
            0
            • O o m n i

              I have an array of bytes. I need to remove the first 16 bytes of the array, then append another byte array onto it. What do you suggest is the best way to do this?

              A Offline
              A Offline
              April Fans
              wrote on last edited by
              #6

              If the size is very large and performance is very important, I suggest you create a new data structure: class ExArray{ byte[] data; int start; int end; public this[int i] { get { return data[(start + i) % data.Length]; } set { data[(start + i) % data.Length = value; } } public Length { get { return end - start >= 0 ? end - start : end - start + data.Length; }} public void Shift(...){...} public void Slice(...){...} }

              April Comm100 - Leading Live Chat Software Provider

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups