1

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.

  • have a look at http://stackoverflow.com/questions/409256/working-with-byte-arrays-in-c and http://stackoverflow.com/questions/2871/reading-a-c-c-data-structure-in-c-from-a-byte-array – Rex Logan Jul 08 '09 at 04:01

3 Answers3

2

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().

David Duncan
  • 1,225
  • 8
  • 14
colithium
  • 10,269
  • 5
  • 42
  • 57
1

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
  • 26,248
  • 10
  • 35
  • 48
0

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
  • 37,839
  • 27
  • 91
  • 125