471,315 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

Combine byte[]'s

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

Dec 28 '05 #1
3 1727

"Vinny Vinn" <Vi*******@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
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

Dec 28 '05 #2
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);
"Vinny Vinn" wrote:
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

Dec 28 '05 #3
Thank you Mythran and Luc

"Vinny Vinn" wrote:
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

Dec 28 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by TJO | last post: by
1 post views Thread by William Stacey [MVP] | last post: by
8 posts views Thread by frekster | last post: by
3 posts views Thread by Schroeder, AJ | last post: by
2 posts views Thread by chris | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.