3.long与byte数组的互转
/**
* 将64位的long值放到8字节的byte数组
* @param num
* @return 返回转换后的byte数组
*/
public static byte[] longToByteArray(long num) {
byte[] result = new byte[8];
result[0] = (byte) (num >>> 56);// 取最高8位放到0下标
result[1] = (byte) (num >>> 48);// 取最高8位放到0下标
result[2] = (byte) (num >>> 40);// 取最高8位放到0下标
result[3] = (byte) (num >>> 32);// 取最高8位放到0下标
result[4] = (byte) (num >>> 24);// 取最高8位放到0下标
result[5] = (byte) (num >>> 16);// 取次高8为放到1下标
result[6] = (byte) (num >>> 8); // 取次低8位放到2下标
result[7] = (byte) (num); // 取最低8位放到3下标
return result;
}
/**
* 将8字节的byte数组转成一个long值
* @param byteArray
* @return 转换后的long型数值
*/
public static long byteArrayToInt(byte[] byteArray) {
byte[] a = new byte[8];
int i = a.length - 1, j = byteArray.length - 1;
for (; i >= 0; i--, j--) {// 从b的尾部(即int值的低位)开始copy数据
if (j >= 0)
a[i] = byteArray[j];
else
a[i] = 0;// 如果b.length不足4,则将高位补0
}
// 注意此处和byte数组转换成int的区别在于,下面的转换中要将先将数组中的元素转换成long型再做移位操作,
// 若直接做位移操作将得不到正确结果,因为Java默认操作数字时,若不加声明会将数字作为int型来对待,此处必须注意。
long v0 = (long) (a[0] & 0xff) << 56;// &0xff将byte值无差异转成int,避免Java自动类型提升后,会保留高位的符号位
long v1 = (long) (a[1] & 0xff) << 48;
long v2 = (long) (a[2] & 0xff) << 40;
long v3 = (long) (a[3] & 0xff) << 32;
long v4 = (long) (a[4] & 0xff) << 24;
long v5 = (long) (a[5] & 0xff) << 16;
long v6 = (long) (a[6] & 0xff) << 8;
long v7 = (long) (a[7] & 0xff);
return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7;
}
4.float与byte数组的互转
/**
* float转换byte
*
* @param bb
* @param x
* @param index
*/
public static void putFloat(byte[] bb, float x, int index) {
// byte[] b = new byte[4];
int l = Float.floatToIntBits(x);
for (int i = 0; i < 4; i++) {
bb[index + i] = new Integer(l).byteValue();
l = l >> 8;
}
}
/**
* 通过byte数组取得float
*
* @param bb
* @param index
* @return
*/
public static float getFloat(byte[] b, int index) {
int l;
l = b[index + 0];
l &= 0xff;
l |= ((long) b[index + 1] << 8);
l &= 0xffff;
l |= ((long) b[index + 2] << 16);
l &= 0xffffff;
l |= ((long) b[index + 3] << 24);
return Float.intBitsToFloat(l);
}