byte[]当初object传入怎么转回成byte[]
试试这个呢
Java code
Object a = new Object();
byte[] b = a.toString().getBytes();
看你是怎么把Object变成byte[]的,逆过程。举个例子接待方案
Java code
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.Random;
public class ArrayDemo {
public static void main(String[] args) {
Apple a = new Apple();
a.color = "red";
a.type = 3;
//now get the byte array by a apple instance
byte[] bytes = a.toBytes();
//get the instance by the byte array
Apple b = Apple.getInstance(bytes);
System.out.println(a.equals(b));
}
}
class Apple{
public String color;
public byte type;
public byte[] toBytes(){
byte[] colorBytes = color.getBytes();
byte[] bytes = new byte[colorBytes.length+1];
System.arraycopy(colorBytes, 0, bytes, 0, colorBytes.length);
bytes[bytes.length-1] = type;
return bytes;
}
public static Apple getInstance(byte[] bytes){
Apple apple = new Apple();
apple.color = new String(bytes, 0, bytes.length-1);
apple.type = bytes[bytes.length-1];
return apple;
}
@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
if(obj==this)
return true;
if(!(obj instanceof Apple))
return false;
Apple b = (Apple) obj;
return b.color.equals(this.color) && this.type==b.type;
}
}
byte[] b = (byte[]) object; 绝对可行