我知道在J2ME中,我可以byte[]
使用该getBytes()
方法从 String 对象中获取对象。我的问题是:是否可以byte[]
从任何其他对象获取对象class type
?另外:是否可以byte[]
从用户定义的类对象中获取对象?
问问题
655 次
2 回答
6
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();
于 2012-04-05T11:33:21.393 回答
1
getBytes();
是 String 类中的方法,它转换你的String into byte Array
..
因此,如果你想在你的类中赋予这种类型的转换函数,
user-defined class object
那么你必须自己植入该函数。
就像 EX:-
public class MyClass{
public byte[] myConvertor(String str){
// do your logic here ...
}
}
于 2012-04-05T11:42:52.910 回答