Best way to read/write binary data

J

Jesse Houwing

Hey all,

I'm inheriting an old project and it contains a piece of code that is
used to read/write an object tree to a binary file. We want to keep the
actual file format in tact, but I'm looking for an easier way to do the
serialization. I've been looking at the Binary Serializer (but it
includes a lot of type information), which doesn't seem to apply here.

I also looked at the Interop namespace and found that the way structs
are formatted/convertedfor interop calls comes pretty close to what I need.

Does anyone have a link or an example on how I could convert a tree of
structs into a simple binary file and read them back out again?

The current solution just creates a byte array and populates the values,
then writing the array to a stream and vice versa.
 
P

Peter Duniho

Hey all,

I'm inheriting an old project and it contains a piece of code that is
used to read/write an object tree to a binary file. We want to keep the
actual file format in tact, but I'm looking for an easier way to do the
serialization. I've been looking at the Binary Serializer (but it
includes a lot of type information), which doesn't seem to apply here.

I also looked at the Interop namespace and found that the way structs
are formatted/convertedfor interop calls comes pretty close to what I
need.

Does anyone have a link or an example on how I could convert a tree of
structs into a simple binary file and read them back out again?

The current solution just creates a byte array and populates the values,
then writing the array to a stream and vice versa.

You can use the struct-related attributes to force a specific layout in
your struct, then use interop to marshal the struct to a byte array, and
then use the standard file i/o methods to write the byte array out.
Reading is simply the reverse.

Alternatively, just use the BinaryWriter and BinaryReader classes, copying
primitive values from/to specific fields in a class individually, and
decomposing more complex types to their primitive values as necessary (if
any).

Pete
 
J

Jesse Houwing

* Peter Duniho wrote, On 10-9-2009 19:07:
You can use the struct-related attributes to force a specific layout in
your struct, then use interop to marshal the struct to a byte array, and
then use the standard file i/o methods to write the byte array out.
Reading is simply the reverse.

Hey Pete, This way exactly what I was trying to do. Do you, by any
chance, have a class/method I could look up in google or msdn to get me
started?
Alternatively, just use the BinaryWriter and BinaryReader classes,
copying primitive values from/to specific fields in a class
individually, and decomposing more complex types to their primitive
values as necessary (if any).

That isn't much better than what's going on right now, but it might be
what I need to 'marshal' classes to byte arrays. Maybe I'll write my own
serializer based on the Marshal classes to accomplish just that.
 
P

Peter Duniho

[...]
You can use the struct-related attributes to force a specific layout in
your struct, then use interop to marshal the struct to a byte array, and
then use the standard file i/o methods to write the byte array out.
Reading is simply the reverse.

Hey Pete, This way exactly what I was trying to do. Do you, by any
chance, have a class/method I could look up in google or msdn to get me
started?

Sure...you probably want to start with the StructLayoutAttribute attribute:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx

The example given on that page is dealing with using the layout control to
interface with unmanaged code. But I'm sure there are other examples
around more related to your specific use (there might even be past
discussions in this newsgroup...but I don't recall for sure if someone
else has discussed precisely what you're asking about).
That isn't much better than what's going on right now, but it might be
what I need to 'marshal' classes to byte arrays. Maybe I'll write my own
serializer based on the Marshal classes to accomplish just that.

I think there's some value to disconnecting your in-memory representation
from the on-disk representation. So in that respect, the
BinaryWriter/Reader classes are actually a good choice. But if you want
to use struct layout instead, it should work fine.

Pete
 
C

Colbert Zhou [MSFT]

Hello Jesse

The managed support service of the newsgroup
microsoft.public.dotnet.language.csharp is now available instead on C#
forum: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/threads.

If you need future support on this issue, would you please repost the
question in the forum with the Windows Live ID used to access your
Subscription benefits? Our engineers will assist you in the new platform.
The article http://msdn.microsoft.com/en-us/subscriptions/aa974230.aspx
introduces more information about the migration. In the future, please post
your C#-related questions directly to the forums. If you have any questions
or concerns, please feel free to contact us: (e-mail address removed).

Regards,
Ji Zhou
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup and Forum support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top