long、int与byte数组之间的相互转换


byte数组转换成long
public long byteArrayToLong(byte[] data) throws IOException {
ByteArrayInputStream bai = new ByteArrayInputStream(data);
DataInputStream dis =new DataInputStream(bai);
return dis.readLong();
}

byte数组转换成int
public int byteArrayToInt(byte[] data) throws IOException {
ByteArrayInputStream bai = new ByteArrayInputStream(data);
DataInputStream dis =new DataInputStream(bai);
return dis.readInt();
}

long转换成byte数组
public byte[] longToByteArray(long l) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos =new DataOutputStream(bao);
dos.writeLong(l);
byte [] buf =bao.toByteArray();
return buf;
}

int转换成byte数组
public byte[] intToByteArray(int a) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos =new DataOutputStream(bao);
dos.writeInt(a);
byte [] buf =bao.toByteArray();
return buf;
}

 

 

posted @ 2017-07-08 12:20  samcheng  阅读(4250)  评论(0编辑  收藏  举报