当前位置: 代码迷 >> J2SE >> byte [] 转换成object 怎么转回来
  详细解决方案

byte [] 转换成object 怎么转回来

热度:1930   发布时间:2013-02-25 00:00:00.0
byte [] 转换成object 如何转回来
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;    }}
------解决方案--------------------------------------------------------
new String()
------解决方案--------------------------------------------------------
byte[] b = (byte[]) object;
  相关解决方案