473,387 Members | 1,798 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,387 software developers and data experts.

how can i translate a object of my defined class to byte[]

lqs
hi£¡
i am writing a program using Socket . There is a class , for example:

public class Student
{
public int nAge;
public string strFirstName;
public string strLastName;
}

Student s=new Student();
s.nAge=22;
s.strFirstName="victor";
s.strLastName="Smith";
then ,before i send the 's' by send() function,how do i translate this
object (i mean 's') to byte[]?
Because the parameter of this send() is byte[].
Thank you for your help!

Nov 16 '05 #1
2 1421
You could serialize your object into a memory stream:

byte[] bytes;

using (MemoryStream stream = new MemoryStream())

{

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, s);

bytes = stream.GetBuffer();

Console.WriteLine(BitConverter.ToString(bytes));

// Get the object back

stream.Position = 0;

Student clone = (Student)formatter.Deserialize(stream);

}

For this code to work you need to apply SerializableAttribute to your
Student class:

[Serializable]
public class Student
....

HTH,
Alexander Shirshov
Nov 16 '05 #2
lqs
Thank you!
"Alexander Shirshov" <al*******@omnitalented.com> дÈëÓʼþ
news:um**************@TK2MSFTNGP14.phx.gbl...
You could serialize your object into a memory stream:

byte[] bytes;

using (MemoryStream stream = new MemoryStream())

{

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, s);

bytes = stream.GetBuffer();

Console.WriteLine(BitConverter.ToString(bytes));

// Get the object back

stream.Position = 0;

Student clone = (Student)formatter.Deserialize(stream);

}

For this code to work you need to apply SerializableAttribute to your
Student class:

[Serializable]
public class Student
...

HTH,
Alexander Shirshov

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ishekara | last post by:
Hi all, An object of a class A which has no member variables and no member functions, still the size of the object is 1 byte, if there is a byte member variable then also the size of the object...
9
bvdet
by: bvdet | last post by:
I have done some more work on a simple class I wrote to calculate a global coordinate in 3D given a local coordinate: ## Basis3D.py Version 1.02 (module macrolib.Basis3D) ## Copyright (c) 2006...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
4
by: kovariadam | last post by:
Hi, Does anybody know why i get this error: SQL0176N The second, third or fourth argument of the TRANSLATE scalar function is incorrect. SQLSTATE=42815 with this query: SELECT...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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...

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.