473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1664
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
7
by: Rolf Hemmerling | last post by:
Hello ! Beginner's question: What ist the easiest way to store and save objects in a file generated by a C++ program, by using the "standard C++ library" and/or "Standard Template Library (...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
1
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image...
4
by: Dale | last post by:
I am creating GIF images with transparent backgrounds on-the-fly for a web app and rendering them by using System.Drawing.Image.Save(Response.OutputStream, ImageType.GIF). I am confident that...
9
by: linyanhung | last post by:
A int in memory takes 32bits (4 bytes). But if I use ofstream to save a int like this: int i=1234567890; ofstream ofs("c:\\intdata.bin", ios::binary); ofs<<i; It becomes 10 bytes in a...
4
by: Richard Lewis Haggard | last post by:
I have an application that can't use the registry to save various user options and application settings. My first thought was to simply use an application configuration file but this approach seems...
4
by: Mr Gray | last post by:
Hi Guys, My requirement is to scan an FTP directory for the presence of 4 files and if a specific file exists I can begin to GET those files, read the contents and save the contents into an SQL...
1
by: Chunekit Pong | last post by:
How to save a Base64 encoded string to a binary file For instance, the following XML tag stores the binary data in a Base64 encoded string <data>TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx<data> ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.