Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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??
0
Solarin
Asked:
Solarin
  • 4
  • 2
  • 2
  • +4
1 Solution
 
Venci75Commented:
use ObjectOutputStream.writeObject()
0
 
CEHJCommented:
This should tell you all you need to know:
http://java.sun.com/docs/books/tutorial/essential/io/serializing.html 
 
0
 
SolarinAuthor Commented:
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.. :(
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!

 
CEHJCommented:
In that case, chain the ObjectOutputStream above a ByteArrayOutputStream and use the latter's toBytesArray to get the array of bytes
0
 
CEHJCommented:
>>toBytesArray

should have been

>>toByteArray
0
 
umangjoshiCommented:
implements java.io.Serializable in MyClass
and user ObjectOutputStream to send it
0
 
TimYatesCommented:
> implements java.io.Serializable in MyClass
> and user ObjectOutputStream to send it

Please don't repeat things that have already been said...
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Try:

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

Then make a DatagramPacket with this byte array.

Mayank.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
How about rating it now?
0
 
CleanupPingCommented:
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.
0
 
TimYatesCommented:
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
0
 
CEHJCommented:
:-)
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now