This question is asked out of curiousity rather than any urgent need. I found some code to convert an object to a byte array (I thought I needed it at the time).
Using commons-codec I noticed that the encoded Hex string representation of the pure byte array was different to what I got if I passed the byte array through the "toByteArray" method below. I noticed that the longer version ends with the shorter version of the Hex string representation.
Instinctively this does not seem right, why does this happen?
What do the extra bytes found via the "toByteArray" method of conversion represent?
I'm guessing it is something to do with encoding?
Many Thanks, I hope this isn't too much of a newbie question.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import org.apache.commons.codec.binary.Hex;
public class Test {
public static void main(String[] args) throws IOException {
byte[] bytes = "Stackoverflow".getBytes();
System.out.println(Hex.encodeHexString(bytes));
System.out.println(Hex.encodeHexString(toByteArray(bytes)));
}
public static byte[] toByteArray(Object obj) throws IOException {
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
oos.close();
bos.close();
bytes = bos.toByteArray();
return bytes;
}
}
RESULT
537461636b6f766572666c6f77 aced0005757200025b42acf317f8060854e002000078700000000d537461636b6f766572666c6f77
The second encoding is much longer because it is an object serialization format whereas the first case is just the content. The Object serialization has the header, the type of the object and finally the content in the object (you will notice the end is the same)