Appending byte array to a byte array
-
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.
-
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.
-
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.
-
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.
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 aList<byte>
and use theAdd
method to copy a byte orAddRange
to copy other byte arrays into it, then callToArray
on theList<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) -
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.
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
-
byte[] byteArray1 = new byte[10]; byte[] byteArray2 = new byte[10]; List<byte> byteList = new List<byte>(byteArray1); byteList.AddRange(byteArray2); byte[] byteArrayAll = byteList.ToArray();
-
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