Hy 얘들 아! 나는 자바에서 직렬화에 대한 몇 가지 오래된 게시물을 찾고 있었다. 개체를 ByteArray로 변환하려고합니다.ByteArray에 개체 serialize
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]);
}
}
}
Aparently가 잘 작동하지만이 ByteArray에 "쓰레기"많이 소개 : 지금까지 I've는이 작업을 수행. 실제로 관심있는 값은 "int x = 1061109744;"에 해당하는 마지막 4 바이트입니다.
왜 그런가?
"정크"값을 피할 수 있습니까?
"서명 된"값을 초과 할 수 있습니까? (128보다 큰 바이트 값을 써야 함)
도와주세요.
을 당신은 내가 같이 여기 http://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array –