Solved

object to byte[] in c#

Posted on 2006-11-12
7
3,892 Views
Last Modified: 2008-01-09
Hi All!

I'm working with c# and would like to put an object type into a byte[].  I found some documentation on how to put a struct into a buffer and how to dynamically allocate memory to copy a void* to another void*, but not how to copy an object into a buffer.  I also looked at MemoryStream and BitConverter.  If anyone has any ideas, I'm more than happy to hear them!

Thanks!

choo_chu
0
Comment
Question by:choo_chu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17925633
Hi, do you want to write the object out to disk and then later read it back in? I think your refering to serialization, this article gives a good explanation using the BinaryFormatter class:

http://www.codeproject.com/csharp/objserial.asp
0
 
LVL 1

Author Comment

by:choo_chu
ID: 17925920
I have looked at the BinaryFormatter, but I'm not looking for serialization because I don't need all the extra information that is needed to reproduce the class when it's deserialized.  I just need the data.  (Notice that sizeof() and the amount of bytes used to serialize and object aren't the same.)  In c++, what I'm looking for would be akin to memcpy().


Thanks!

choo_chu
0
 
LVL 7

Expert Comment

by:yotamsher
ID: 17925944
Hey choo_chu

what does your class look like?
I got the feeling, that it is more like a struct, can't you just write a ToByteArray and FromByteArray functions?
why do you need to memcpy? are you then transfering it somewhere?

the more details you give, the better answers you'll get

Yotam
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:choo_chu
ID: 17926073
Hi,

I won't know what the incoming object looks like, I will only know its size.  The constructor has the following definition:
            DBObject( object obj, int size )

The class DBObject is the class of objects that are in the nodes of a b-tree.  The class contains two data members, _size and _data.  The constructor will set values _size = size and _data = obj...I want to be able to copy obj into _data, which is a byte[].  The data in this object (_size and _data) will be written to file and later retrieved.  

Thanks!

choo_chu
0
 
LVL 7

Accepted Solution

by:
yotamsher earned 500 total points
ID: 17926226
Hey choo_chu
my gfeeling, is that you should be going in the direction of serialization, as offered by  Zephyr__
  )-:
but look at this article
    http://www.codeguru.com/csharp/csharp/cs_syntax/reflection/article.php/c5885/
it might give you a hint to a different road:

Yotam
0
 
LVL 1

Author Comment

by:choo_chu
ID: 17926357
Thanks for the article Yotam...I think that will help a lot.  I'll keep you posted.

choo_chu
0
 
LVL 1

Author Comment

by:choo_chu
ID: 17926854
Ok, I figured out how to do what I want using reflection...it's actually kinda neat ;)  This isn't all the code, but is the jist of what I did.  I got the field type, and I stuff the values into the byte[] using BitConverter.GetBytes().  When I get to the byte[] type, I just use the GetValue() supplied by the FieldInfo.  Mind you this is test code, so it doesn't pay attention to where in the byte[] it is putting things, it is just over writing previous values.  It is just so I could see whether this idea would actually work.


                              // copy to byte[]
                              switch( fieldType )
                              {
                                    case( "System.Int32" ): // int or long
                                          if( fi[c].Name.ToString() == "_size" )
                                          {
                                                size = (int)fi[c].GetValue( obj );
                                          }
                                          _data = BitConverter.GetBytes( size );
                                          break;
                                    case( "System.Int64" ):
                                          _data = BitConverter.GetBytes( (long)fi[c].GetValue( obj ) );
                                          break;
                                    case( "System.Boolean" ):
                                          _data = BitConverter.GetBytes( (bool)fi[c].GetValue( obj ) );
                                          break;
                                                                                                                   :
                                                                                                                   :

Thanks a lot for your input!

choo_chu
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iSeries email authority 6 89
Developing a front end to SPLUNK 1 57
WordPress: Debugging from my Windows 10 Desktop 6 62
Java array 10 57
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question