Hex encoded form of byte array different to same byte array “object” converted to byte array. Why?

StackOverflow https://stackoverflow.com/questions/5485836

문제

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)

다른 팁

An ObjectOutputStream is able to serialize any type of object (as long as it is Serializable). So, it can't really map any byte[] to itself, since this would imply there is no room for other objects.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top