When I need to serialize things to a byte array I usually write the array to a MemoryStream first then use the binary formatter to serialize it to whatever (file, another byte array whatever).

The question I had was, can I just serialize a byte array instead of converting it to a memory stream and then back again?

The binary formatter says it needs a "stream" to serialize...is there another method?

Recommended Answers

All 4 Replies

When I need to serialize things to a byte array I usually write the array to a MemoryStream first then use the binary formatter to serialize it to whatever (file, another byte array whatever).

The question I had was, can I just serialize a byte array instead of converting it to a memory stream and then back again?

The binary formatter says it needs a "stream" to serialize...is there another method?

BinaryFormatter.Serialize writes to a stream, so if you want to serialize an object to a byte array, using a MemoryStream is the simplest way to go.

I'm not sure what "write the array to a MemoryStream first" is intended to accomplish; you can create an expandable MemoryStream without creating a byte array first.

BinaryFormatter.Serialize writes to a stream, so if you want to serialize an object to a byte array, using a MemoryStream is the simplest way to go.

I'm not sure what "write the array to a MemoryStream first" is intended to accomplish; you can create an expandable MemoryStream without creating a byte array first.

What I meant by that was, I started with a byte array to begin with. But since the BinaryFormatter requires a stream, I have to copy the array to the stream then serialize the stream.

When things come in over a network on a socket they are transferred as a byte array and if I need to serialize that array that I received into whatever, a file, for example, I need to copy that array to a memory stream and then serialize the memory stream...seems redundant to me. I wanted to know if there was a way to serialize an array of bytes, as in "byte[]."

I was always under the impression that a memory stream is just a glorified byte array that can expand and contract depending on how you use it, where as a byte array (byte[]) you can't do that, you need to create a new one with the new size and then copy whatever you need to the new one.

I guess what I was asking is, can I take a byte or an array of bytes in the form of byte[] and either with the CLR or through a method of my own, write those bytes to a file to be read later without copying the bytes to another object.

Why do you need to serialize a byte array into a byte array (which is what Binary Formatter really is)? Just transfer the array, don't serialize it.

And yes, you can write a byte array to a file (and read it back) with File.WriteAllBytes (and File.ReadAllBytes)

commented: That's what I was looking for! Sweeeeeet! Thanks! +5

Awesome! That's exactly what I was looking for!
Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.