471,582 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How do I save a class's Raw Data (without .NET object data) to a binary file?

I want to save the raw data of a class (without the .NET object
overhead) to a binary file. For example:
ref class Test
{
public:
String^ name;
Int32 number;
.....
}

I want to save the raw characters of the string and the raw integer
without the additional .NET Object data. I am trying to create an
array of Bytes and cast that array as my class, but the casting fails.
I was then going to write the Array out using a BinaryFormater object,
but I first need to cast the array as my class so I can easily stuff
the array. Can anyone help by providing an example of casting an array
of Bytes to a managed class, or of writing the actual raw data of a
class to a file?

Jun 27 '06 #1
2 1564
<mp********@gmail.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
I want to save the raw data of a class (without the .NET object
overhead) to a binary file. For example:
ref class Test
{
public:
String^ name;
Int32 number;
....
}

I want to save the raw characters of the string and the raw integer
without the additional .NET Object data. I am trying to create an
array of Bytes and cast that array as my class, but the casting fails.
I was then going to write the Array out using a BinaryFormater object,
but I first need to cast the array as my class so I can easily stuff
the array. Can anyone help by providing an example of casting an array
of Bytes to a managed class, or of writing the actual raw data of a
class to a file?


If there is some reason that you can not use the canonical method defined
here:

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

and if you are proficient in C++, then you could use the PtrToStringChars()
function

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

to get a pointer to the zero terminated array of characters that comprise
the string in your object. That done you could serialize them and the
integer however you like. Of course, then you will need to take
responsibility for your proprietary the deserialization as well.

Regards,
Will
Jun 27 '06 #2
<mp********@gmail.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
I want to save the raw data of a class (without the .NET object
overhead) to a binary file. For example:
ref class Test
{
public:
String^ name;
Int32 number;
....
}

I want to save the raw characters of the string and the raw integer
without the additional .NET Object data. I am trying to create an
array of Bytes and cast that array as my class, but the casting fails.
I was then going to write the Array out using a BinaryFormater object,
but I first need to cast the array as my class so I can easily stuff
the array. Can anyone help by providing an example of casting an array
of Bytes to a managed class, or of writing the actual raw data of a
class to a file?


That's not going to happen. However you can treat your object as an array
of bytes. But you must allocate the buffer with the class type. You would
also get further with a value class than a ref class.

This is by nature not verifiably type-safe. Count on using unsafe code
(pointers). Furthermore, the string is not contained inside your object,
only a gc pointer (reference), which can be modified by the gc at any time,
unless you use pin_ptr.

However, something like this should get you the binary data for any field:
Test^ that = gcnew Test();
// fill in that
pin_ptr<Int32> pRaw = &that->number;
BYTE* pBytes = (BYTE*)pRaw;
Jun 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Sebastian Kemi | last post: by
1 post views Thread by liuliuliu | last post: by
9 posts views Thread by linyanhung | last post: by
1 post views Thread by Chunekit Pong | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

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.