473,503 Members | 8,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert object to byte[]

18 New Member
Hi All,

I am stuck with a unique problem, I have a class whose object i need to convert to a byte[] to send it further.

I am using microsoft's Binaryformatter to convert object to byte[] as per given below.

But problem here is that the converted byte[] contains 245 bytes of value instead of 22 what the class actually should contain.

Expand|Select|Wrap|Line Numbers
  1. private byte[] ObjectToByteArray(Object obj)
  2. {
  3.       if(obj == null)
  4.                return null;
  5.      BinaryFormatter bf = new BinaryFormatter();
  6.      MemoryStream ms = new MemoryStream();
  7.      bf.Serialize(ms, obj);
  8.     return ms.ToArray();
  9.  }
Can anyone help me out as it is a big hindrance for me to proceed further.

Thanks in Advance
Eric Haas.
Dec 18 '08 #1
2 5781
Plater
7,872 Recognized Expert Expert
I would guess the extra bytes are there to describe the actual datatype. since you are using a class, extra information must be serialized to dictate what the object was before
Dec 18 '08 #2
maya7
10 New Member
I had the same problem, I couldn't find an answer, so I used a different way.
Instead of converting an object, I builded byte[] buffer from the Object's data and sent this buffer. It worked for me.

Expand|Select|Wrap|Line Numbers
  1. Class nObj = (Class )obj.
  2.  
  3.  //you should know the size of the object
  4. byte[] buffer = new byte[MSG_SIZE]; 
  5. int index = 0;
  6.  
  7. //set the data - insert the data in the right order    
  8. buffer[index] = (byte)nObj.m_entId;
  9. //index is updated, depends on the datatype ( int16 in this example )
  10. index +=2;
  11.  
  12. buffer[index] = (byte)nObj.m_bTask;
  13. //index is updated, depends on the datatype ( byte in this example )
  14. index += 1;
  15.  
  16. //you have buffer that holds all the data
  17. return buffer
Good Luck!
Dec 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2723
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
1
4795
by: Lamberti Fabrizio | last post by:
Hi all. I've got an ActiveX Object that gives me OleColor codes for its graphical components. I need to convert these OleColor codes into RGB codes using JScript or VBScript, becuase I have...
6
15297
by: Gator | last post by:
Hi All, Basically my situation is this, I have a server implemented in C++, unmanaged code, using proprietery protocol over TCP/IP to communicate with the cilent(c++ also). Now, I am implementing...
4
49518
by: Serge Klokov | last post by:
Hello! I have a Oracle table with a BLOB field. Each field is actually a simple text file of several lines. How to get this BLOB fields in readable format? I tryied something like...
3
3621
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
4
3272
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
1
5393
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a...
9
12664
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
8
5892
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
19
5308
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.