Link to home
Start Free TrialLog in
Avatar of Solarin
Solarin

asked on

how to convert an object to into a byte

well, I've an object of type MyClass. what I want to do is to convert such object in byte or byte[] so that I can send it through a stream.

Any suggests??
Avatar of Venci75
Venci75

use ObjectOutputStream.writeObject()
Avatar of CEHJ
This should tell you all you need to know:
http://java.sun.com/docs/books/tutorial/essential/io/serializing.html 
 
Avatar of Solarin

ASKER

ok, the problem is quite different.

i've a DatagramPacket(byte[]...); and i've to put the byte value of the object in that field. it's nothing to do with streams.. :(
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>toBytesArray

should have been

>>toByteArray
implements java.io.Serializable in MyClass
and user ObjectOutputStream to send it
> implements java.io.Serializable in MyClass
> and user ObjectOutputStream to send it

Please don't repeat things that have already been said...
Try:

byte arr[] = MyClassObject.toString ().getBytes () ;

Then make a DatagramPacket with this byte array.

Mayank.
How about rating it now?
Solarin:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept CEHJ's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer
:-)