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. Appending byte array to a byte array

Appending byte array to a byte array

Scheduled Pinned Locked Moved C#
data-structuresquestion
7 Posts 3 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.
  • D Offline
    D Offline
    deep7
    wrote on last edited by
    #1

    Hi, I have a function which returns a byte array. This method is being called in a loop. I need to build another byte array from these returned byte array. For eg., in StringBuilder, we can keep appending string to it. Similar way, i need to append byte array to one main byte array. How can this be achieved? Can anyone give me some solution here? Thanks for your time.

    L D 4 Replies Last reply
    0
    • D deep7

      Hi, I have a function which returns a byte array. This method is being called in a loop. I need to build another byte array from these returned byte array. For eg., in StringBuilder, we can keep appending string to it. Similar way, i need to append byte array to one main byte array. How can this be achieved? Can anyone give me some solution here? Thanks for your time.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      byte[] byteArray1 = new byte[10]; byte[] byteArray2 = new byte[10]; List<byte> byteList = new List<byte>(byteArray1); byteList.AddRange(byteArray2); byte[] byteArrayAll = byteList.ToArray();

      D 1 Reply Last reply
      0
      • D deep7

        Hi, I have a function which returns a byte array. This method is being called in a loop. I need to build another byte array from these returned byte array. For eg., in StringBuilder, we can keep appending string to it. Similar way, i need to append byte array to one main byte array. How can this be achieved? Can anyone give me some solution here? Thanks for your time.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Create new byte array ,which length is the sum of the source arrays lengths and use System.Array.Copy to copy source data to the result array.

        Life is a stage and we are all actors!

        1 Reply Last reply
        0
        • D deep7

          Hi, I have a function which returns a byte array. This method is being called in a loop. I need to build another byte array from these returned byte array. For eg., in StringBuilder, we can keep appending string to it. Similar way, i need to append byte array to one main byte array. How can this be achieved? Can anyone give me some solution here? Thanks for your time.

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

          Once an array's size is declared, it cannot be changed so you can't append directly to the end. If you know the end size you need then you can declare a new byte array, and use Buffer.BlockCopy to copy from other arrays into the new one specifying offsets where needed etc. If the end size is unknown, I'd use a List<byte> and use the Add method to copy a byte or AddRange to copy other byte arrays into it, then call ToArray on the List<byte> at the end, and you will then have an array of the correct size.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          1 Reply Last reply
          0
          • D deep7

            Hi, I have a function which returns a byte array. This method is being called in a loop. I need to build another byte array from these returned byte array. For eg., in StringBuilder, we can keep appending string to it. Similar way, i need to append byte array to one main byte array. How can this be achieved? Can anyone give me some solution here? Thanks for your time.

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

            I've been pondering on this a little more - these two methods may help. The first will append one or more individual items, the second one or more collections of items. These may not be the fatest, but they work.

            public static T[] AddElements<T>(T[] currentArray, params T[] element)
            {
            T[] result = new T[currentArray.Length + element.Length];
            Buffer.BlockCopy(currentArray, 0, result, 0, currentArray.Length);
            for (int i = 0; i < element.Length; i++)
            result[currentArray.Length + i] = element[i];
            return result;
            }

            public static T[] AddElements<T>(T[] currentArray, params IEnumerable<T>[] elements)
            {
            List<T> resultList = new List<T>(currentArray);
            foreach (IEnumerable<T> item in elements)
            resultList.AddRange(item);
            return resultList.ToArray();
            }

            Some examples of useage:

            byte[] currentArray = new byte[] { 0, 1, 2, 3, 4 };
            byte elementA = 5;
            byte elementB = 6;
            byte elementC = 7;
            currentArray = AddElements(currentArray, elementA);
            currentArray = AddElements(currentArray, elementB, elementC);
            byte[] elementsA = new byte[] { 8, 9, 10 };
            byte[] elementsB = new byte[] { 11, 12, 13 };
            List<byte> elementsC = new List<byte>() { 14, 15, 16 };
            currentArray = AddElements(currentArray, elementsA);
            currentArray = AddElements(currentArray, elementsB, elementsC);

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            modified on Wednesday, August 26, 2009 9:00 AM

            D 1 Reply Last reply
            0
            • L Lost User

              byte[] byteArray1 = new byte[10]; byte[] byteArray2 = new byte[10]; List<byte> byteList = new List<byte>(byteArray1); byteList.AddRange(byteArray2); byte[] byteArrayAll = byteList.ToArray();

              D Offline
              D Offline
              deep7
              wrote on last edited by
              #6

              Thanks :) this worked fine

              1 Reply Last reply
              0
              • D DaveyM69

                I've been pondering on this a little more - these two methods may help. The first will append one or more individual items, the second one or more collections of items. These may not be the fatest, but they work.

                public static T[] AddElements<T>(T[] currentArray, params T[] element)
                {
                T[] result = new T[currentArray.Length + element.Length];
                Buffer.BlockCopy(currentArray, 0, result, 0, currentArray.Length);
                for (int i = 0; i < element.Length; i++)
                result[currentArray.Length + i] = element[i];
                return result;
                }

                public static T[] AddElements<T>(T[] currentArray, params IEnumerable<T>[] elements)
                {
                List<T> resultList = new List<T>(currentArray);
                foreach (IEnumerable<T> item in elements)
                resultList.AddRange(item);
                return resultList.ToArray();
                }

                Some examples of useage:

                byte[] currentArray = new byte[] { 0, 1, 2, 3, 4 };
                byte elementA = 5;
                byte elementB = 6;
                byte elementC = 7;
                currentArray = AddElements(currentArray, elementA);
                currentArray = AddElements(currentArray, elementB, elementC);
                byte[] elementsA = new byte[] { 8, 9, 10 };
                byte[] elementsB = new byte[] { 11, 12, 13 };
                List<byte> elementsC = new List<byte>() { 14, 15, 16 };
                currentArray = AddElements(currentArray, elementsA);
                currentArray = AddElements(currentArray, elementsB, elementsC);

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                modified on Wednesday, August 26, 2009 9:00 AM

                D Offline
                D Offline
                deep7
                wrote on last edited by
                #7

                Thanks for your reply :)

                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