views:

488

answers:

3

I have a raw buffer with it i need to make 3 others, the head which is always the first 8 bytes, body which is always from byte 8 to ? then foot which is from ? to the end of he file.

How do i make a buffer from an already existing buffer so i can fill in body and foot. also how do i create head to use the first 16 bytes. I am assuming i am not using a ref or pointer.

+2  A: 

You can use Array.Copy() to copy elements from one array to another. You can specify start and end position from source and for destination,

You may also want to check out Buffer.BlockCopy()

colithium
A: 

Try using Buffer.BlockCopy, which should provide faster performance for primitive types compared to Array operations. (why? I don't know but MSDN said so)

Jeff Meatball Yang
+1  A: 

You can use a BinaryReader from a MemoryStream

 System.IO.MemoryStream stm = new System.IO.MemoryStream( buf, 0, buf.Length );
 System.IO.BinaryReader rdr = new System.IO.BinaryReader( stm );

 int bodyLen = xxx;
 byte[] head = rdr.ReadBytes(8)
 byte[] body = rdr.ReadBytes(bodyLen );
 byte[] foot = rdr.ReadBytes(buf.Length-bodylen-8);
Rex Logan