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

I know that in J2ME I can get a byte[]

String object using a method getBytes()

. My question is, is it possible to get an object byte[]

from any other class type

? Also: is it possible to get an object byte[]

from a custom class object?

+3


source to share


2 answers


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

A mock service may be implemented in some classes.

Can I get a byte [] object from a custom class object?

It's not without you that you yourself record the conversion.




An example of how to do it yourself (just note what DataOutputStream

handles the conversion, such as which byte order 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();

      

+7


source


getBytes();

      

- a method in the String class that converts your String into byte Array

..
Therefore, if you want to give this type of conversion function in your class,   user-defined class object


then you must implant this function. As EX: -



public class MyClass{
    public byte[]  myConvertor(String str){
        // do your logic here ... 
    }
}

      

+1


source







All Articles