[RESOLVED] Serializing to Byte array
I'm trying to take a structure, and turn it into an array of Bytes so that I can send it via UDP. It seems like this should be a case of using a BinaryFormatter to serialize the structure, but I can't see any good way of turning this into an array of Bytes. Any suggestions?
Re: Serializing to Byte array
This is just a wild guess, havent even tested it properly.:o
VB.Net Code:
Dim c As New Class1
Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim memStream As New IO.MemoryStream
formatter.Serialize(memStream, c)
Using b As New IO.BinaryReader(memStream)
Dim bte(b.BaseStream.Length) As Byte
b.Read(bte, 0, b.BaseStream.Length)
End Using
memStream.Close()
Re: Serializing to Byte array
Thanks, it looks quite plausible, I'll look it up. The MemoryStream was the step I was missing. I got thinking about it as I was driving around today, and realized I should pursue something close to that (a filestream), but I had forgotten the MemoryStream (that's ironic), which seems more likely.
Re: Serializing to Byte array
Finally got to test this, and it worked well. Oddly, BaseStream.Length returns a Long (ok, I guess that's not really all that odd, but it IS a huge number), so it had to be cast to an integer (it will never be that big in this app).