You haven't said what you are doing with this, but in a lot of byte[]
processing code you work with an offset into the buffer... so, instead of initially setting this offset to 0
, you would set it to the length of "same bytes".
If you are wrapping in MemoryStream
, you could just set the Position
forward to that number before working with it.
Finally, you could just copy the desired data out, perhaps using Buffer.BlockCopy
, specifying the start offset. This would be my least preferred option, as the second buffer and block copy is redundant (we already have the data and know where we want to look).
Examples:
// invent some initial data
byte[] data = Encoding.ASCII.GetBytes("samebytesDataNeededIsHere");
int fixedOffset = 9; // length of samebytes
// as a segment
ArraySegment<byte> segment = new ArraySegment<byte>(data,
fixedOffset, data.Length - fixedOffset);
// as a separate buffer
byte[] copy = new byte[data.Length - fixedOffset];
Buffer.BlockCopy(data, fixedOffset, copy, 0, copy.Length);
// as a stream
var ms = new MemoryStream(data, fixedOffset, data.Length - fixedOffset);
// or just directly
for(int i = fixedOffset ; i < data.Length ; i++) {
// access data[i]
}