|
-
Jul 2nd, 2007, 11:55 AM
#1
[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?
My usual boring signature: Nothing
 
-
Jul 2nd, 2007, 12:14 PM
#2
Re: Serializing to Byte array
This is just a wild guess, havent even tested it properly.
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()
-
Jul 2nd, 2007, 07:16 PM
#3
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.
My usual boring signature: Nothing
 
-
Jul 4th, 2007, 07:28 PM
#4
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).
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|