473,395 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Append a byte[] to a MemoryStream or another byte[]

Is there a class that will allow me to append a byte[] to another? The data
that I have is binary so StringBuilder will not work.
Nov 17 '05 #1
5 34987
Eric,

You can use an array list, or, if you are using .NET 2.0, you can use a
List<byte>.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Eric" <Er**@discussions.microsoft.com> wrote in message
news:97**********************************@microsof t.com...
Is there a class that will allow me to append a byte[] to another? The
data
that I have is binary so StringBuilder will not work.

Nov 17 '05 #2
You can write subsequent arrays of bytes to a MemoryStream, and then use it's
ToArray method to retrieve the concatenated byte array.

Look at the overloads of the Write and related methods:

http://msdn.microsoft.com/library/de...writetopic.asp

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Eric" wrote:
Is there a class that will allow me to append a byte[] to another? The data
that I have is binary so StringBuilder will not work.

Nov 17 '05 #3
Is there a class that will allow me to append a byte[] to another? The data
that I have is binary so StringBuilder will not work.


Appending it to a MemoryStream (per your subject line) is easy, just
call the Stream.Write method.

You can't append things to an array since arrays have a fixed length.
But you can create a new array and write both parts to it using
Array.Copy or Buffer.BlockCopy.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #4
if you want to append to MemoryStream, use Write method, if you want to
merge into two byte[]'s, use static method Array.Copy
"Eric" <Er**@discussions.microsoft.com> wrote in message
news:97**********************************@microsof t.com...
Is there a class that will allow me to append a byte[] to another? The data that I have is binary so StringBuilder will not work.

Nov 17 '05 #5
Eric wrote:
Is there a class that will allow me to append a byte[] to another?
The data that I have is binary so StringBuilder will not work.


Others have suggested a MemoryStream. I won't. The reason is that it
will cause at least one extra array allocation than you need. Just
allocate the array yourself and use Buffer.BlockCopy, it is simple:

byte[] AppendArrays(byte[] a; byte[] b)
{
byte[] c = new byte[a.Length + b.Length]; // just one array
allocation
Buffer.BlockCopy(a, 0, c, 0, a.Length);
Buffer.BlockCopy(b, 0, c, a.Length, b.Length);
return c;
}

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
8
by: steve | last post by:
I'd like to take the value of an int and put the value into a byte array. Basically the hexidecimal representation of an integer, with out having to convert the integer to a string and then to a...
2
by: Dunc | last post by:
Hi, I've got an HttpWebResponse object, all working fine. I'm getting the underlying stream and currently writing it to a file using the ReadToEnd() method. All happy. I want to do a bit of...
0
by: CroDude | last post by:
Hi all! I have problems when writting bitmap to a byte array and after reading it back form byte to Bitmap object. What I do is this: First I throw Bitmap to a memory-stream and then I write it...
1
by: Sharon | last post by:
I have 2 questions that are just the opposite to one another: (1) I need to read an image file (like bitmap, jpeg etc.) and to save only its data, I need to save his data in a raw data format,...
5
by: Lee Gillie | last post by:
I am using Cryptography. You can encrypt or decrypt by providing an output stream as a parameter to the CryptoStream constructor. But I need byte arrays, as I am encrypting on the fly to a socket,...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
3
by: Marc Gravell | last post by:
It might just be my tired eyes, but I can't see what is wrong in the following: I have a byte array filled with random data (with a fixed seed to make reproducable); I then compress and...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.