Insert a byte array into another byte array at a specific position with c#
Solution 1
Use a List<byte>
instead of a byte[]
; it will provide the flexibility you are looking for...
List<byte> b1 = new List<byte>() { 45, 46, 47, 50, 51, 52 };
List<byte> b2 = new List<byte> { 48, 49 };
b1.InsertRange(3, b2);
Then if you need to go back to a byte[]
for whatever reason you can call...
b1.ToArray();
Solution 2
But I would assume this is something common
If inserting a large chunk of data into the middle of another large chunk of data is something you do often then you might consider using a data structure designed to do that. An array is designed to be fixed in size and mutable in content. If your requirement includes "variable in size" then an array is the wrong data type for you. Consider instead a doubly linked list or a catenable deque.
and should be easier?
You've identified a trivial four-step algorithm that does what you want. It doesn't get much easier than that.
Solution 3
Look at Array.CopyTo
.
Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index.
Solution 4
If performance is not important, consider:
var combined = first.Take(insertPosition)
.Concat(second)
.Concat(first.Skip(insertPosition))
.ToArray();
I suppose this is pretty much the four-step algo you've suggested, except the first step "comes at the end." However, do note that this is inefficient for a number of reasons, including a needlessly dynamic buffer and a redundant partial enumeration of the first array.
Otherwise, what you have suggested is perfectly fine.
Remy
Chief of the System at www.supertext.ch, the first copywriting and translation agency on the internet.
Updated on June 13, 2022Comments
-
Remy about 2 years
This might be a silly question, but have not found a simple answer yet...
I'm trying to insert a simple c# byte array into another byte array at a specific position. E.g. the existing bytes should be not be overridden, but just moved further back. Really just like you copy page some text block inside an existing text block.
- So far, I would create a new array with the length of both existing arrays.
- Copy the first array into the new one until the position where the insert starts.
- Add the inserted array
- Add the rest of the existing array.
But I would assume this is something common and should be easier? Or am I wrong?
-
Remy over 13 yearsRight, that would help to implement the flow what I described above. Was hoping that there is a simpler way?
-
Oded over 13 years@Remy - Not really, not if you want to use arrays.
-
Aaron McIver over 13 years-1 CopyTo is going to override the contents based on the starting index; which the OP stated they did not want to do.
-
Eric Lippert over 13 yearsOf course, as an implementation detail, a list is just a view of an array; all of the insertions and removals just do the array copying operations for you.
-
Remy over 13 yearsI should have mentioned, that I'm doing this with files who could get pretty big. But I guess I got the answers I was looking for. Multiple ways would work, but as in these reality TV shows, I had to choose one :-)
-
codymanix over 13 yearsYes, indeed. At the end, class List is doing the same algorithm internally which the OP didn't wanted to write on its own.
-
Brian over 13 years@Remy: Note that
Insert
is an O(n) operation. If you expect to be using many Inserts of very small lists of bytes, you should consider using aLinkedList<T>
. -
Tony over 12 yearsThank you! Very clean and precise.