Combine byte[]'s

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is the simplest way to combine one byte[] with another?
for example if i have
byte[] header = Encoding.ASCII.GetBytes("this is the header");
and
byte[] body = Encoding.ASCII.GetBytes("this is the body");
how can i combined them into one?

TIA,
Vinny
 
Vinny Vinn said:
What is the simplest way to combine one byte[] with another?
for example if i have
byte[] header = Encoding.ASCII.GetBytes("this is the header");
and
byte[] body = Encoding.ASCII.GetBytes("this is the body");
how can i combined them into one?

TIA,
Vinny

:) Here is one way.

byte[] header = System.Text.Encoding.ASCII.GetBytes(
"this is the header"
);
byte[] body = System.Text.Encoding.ASCII.GetBytes(
"this is the body"
);
byte[] contents = new byte[header.Length + body.Length];
Array.Copy(header, 0, contents, 0, header.Length);
Array.Copy(body, 0, contents, header.Length, body.Length);
string s = System.Text.Encoding.ASCII.GetString(contents);
Console.WriteLine(s);

HTH

Mythran
 
I suppose you mean 'concat' the two buffers ?

byte[] concatBuffer = new byte[header.Length + body.Length];

Buffer.BlockCopy(header, 0, concatBuffer, 0, header.Length);
Buffer.BlockCopy(body, 0, concatBuffer, header.Length, body.Length);
 
Back
Top