8字节转化为long的两个方式(高低分字节来分)

字体大小: 中小 标准 ->行高大小: 标准

从低到高位的转化方式

 //byte数组转成long
    public static long byteToLong(byte[] b) {
        long s = 0;
        long s0 = b[0] & 0xff;// 最低位
        long s1 = b[1] & 0xff;
        long s2 = b[2] & 0xff;
        long s3 = b[3] & 0xff;
        long s4 = b[4] & 0xff;// 最低位
        long s5 = b[5] & 0xff;
        long s6 = b[6] & 0xff;
        long s7 = b[7] & 0xff;

        // s0不变
        s1 <<= 8;
        s2 <<= 16;
        s3 <<= 24;
        s4 <<= 8 * 4;
        s5 <<= 8 * 5;
        s6 <<= 8 * 6;
        s7 <<= 8 * 7;
        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
        return s;
    } 

 

从高位到低位的转化方式

 

public static long byte8ToLong(byte[] b, int offset) {
		return ((long) (0xff & b[offset])) << 56
				| ((long) (0xff & b[offset + 1])) << 48
				| ((long) (0xff & b[offset + 2])) << 40
				| ((long) (0xff & b[offset + 3])) << 32
				| (0xff & b[offset + 4]) << 24 
				| (0xff & b[offset + 5]) << 16
				| (0xff & b[offset + 6]) << 8 
				| (0xff & b[offset + 7]);
}

 

 

 

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/70312.html