Java中将Byte[] 转成封装类型
今天做一个需求,需要将byte[]类型转换为Double 和 String,因此整理起来
Double
byte[]转double
public Double byteArrayToDouble(byte[] bytes) {if (bytes.length != 8) {throw new IllegalArgumentException("数据长度不符");}long longBits = 0;for (int i = 0; i < 8; i++) {longBits = (longBits << 8) | (bytes[i] & 0xFF);}return Double.longBitsToDouble(longBits);}
byte[]转String
new String(bytes)
但是bytes的长度有问题