Hy guys! I've been searching for some old posts about Serialization in Java. I want to convert an Object into a ByteArray. So far I´ve done this:
public class test
{
public static void main(String[] args) throws IOException
{
// 00111111 00111111 00111111 11110000 - in bytes: 63, 63, 63, 240
int x = 1061109744;
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(x);
bytes = bos.toByteArray();
}
finally
{
out.close();
bos.close();
}
System.out.println("Lenght: " + bytes.length);
for(int i=0; i < bytes.length; i++)
{
System.out.println(bytes[i]);
}
}
}
Apparently it works good but it introduces a lot of "junk" into the ByteArray
. The values that really have interest to me are the last 4 bytes that correspond to my "int x = 1061109744;".
Why does this happen?
Is it possible to avoid the "junk" values?
Is it possible to surpass the "signed" values? (I have to write byte values bigger than 128).