byte[] buffer or ref/pointer? in C#
Question
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.
Solution
You can use Array.Copy() to copy elements from one array to another. You can specify the start and end positions for the source and destination.
You may also want to check out Buffer.BlockCopy().
OTHER TIPS
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);
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)