Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a byte[] object from another class object in J2ME?

I know that in J2ME I can get a byte[] object from a String object by using the getBytes() method. My question is : is it possible to get a byte[] object from any other class type ? In addition : is it possible to get a byte[] object from a user-defined class object ?

like image 451
pheromix Avatar asked Nov 25 '25 03:11

pheromix


1 Answers

Is it possible to get a byte[] object from any other class type ?

Some classes may implement a simular service.

Is it possible to get a byte[] object from a user-defined class object ?

Not without you writing the conversion yourself.


Example how to do it yourself (just note that the DataOutputStream handles the conversion, for example which byte order that is used):

ByteArrayOutputStream out = new ByteArrayOutputStream();
{
    // conversion from "yourObject" to byte[]
    DataOutputStream dos = new DataOuputStream(out);
    dos.writeInt(yourObject.intProperty);
    dos.writeByte(yourObject.byteProperty);
    dos.writeFloat(yourObject.floatProperty);
    dos.writeChars(yourObject.stringProperty);
    dos.close();
}
byte[] byteArray = out.toByteArray();
like image 158
dacwe Avatar answered Nov 26 '25 16:11

dacwe