JAVA byte[]数组转long

        最近在搞一个读取数据库日志,然后解析实时日志的程序,发现读取的日志id是number类型,但是得到的记录确实byte类型数组,因为byte类型的最大取值范围是-128到127,所以当number的值超出这个范围时候,byte数组就需要增加长度

byte占一个字节,如果不做处理直接付给int或long类型的变量,当高位为1时会导致得到不正确的值(负数), 如果与0xff(或者0xffL)做位与就可以保证得到byte本身的值。以下是byte和long对应的几个例子:

byte[]long
127127
0,-128128
0,-1255
1,0256
1,0,065536

从上表中可以看出当超高127时候byte数组多了一位-128对应int值是128 ,-127对应是129,-126对应是130 ... -1对应是255 当值为256时向前进1即{1,0}

话不多说直接上代码
 

public class Test {
    public static void main(String[] args) {
        byte[] b = {0,-128};
        byte[] b2 = {1,0};
        byte[] b3 = {2,-12,0};
        byte[] b4 = {1,0,0,0};
        byte[] b5 = {1,0,0,0,0};
        System.out.println("b的值是:"+bytesToLong(b));
        System.out.println("b的值是:"+bytes2long(b));
        System.out.println();
        System.out.println("b2的值是:"+bytesToLong(b2));
        System.out.println("b2的值是:"+bytes2long(b2));
        System.out.println();
        System.out.println("b3的值是:"+bytesToLong(b3));
        System.out.println("b3的值是:"+bytes2long(b3));
        System.out.println();
        System.out.println("b4的值是:"+bytesToLong(b4));
        System.out.println("b4的值是:"+bytes2long(b4));
        System.out.println();
        System.out.println("b5的值是:"+bytesToLong(b5));
        System.out.println("b5的值是:"+bytes2long(b5));

    }

    /**
     * 直接读取byte数组进行计算
     * @param bytes
     * @return
     */
    public static long bytesToLong(byte[] bytes) {
        if(bytes.length == 1){
            return bytes[0];
        }else if(bytes.length == 2){
            return bytes[0] * 256 +  byte2Int(bytes[1]);
        }else if(bytes.length == 3){
            return bytes[0] * 65536 +  byte2Int(bytes[1]) * 256 + byte2Int(bytes[2]);
        }else if(bytes.length == 4){
            return bytes[0] * 16777216 + byte2Int(bytes[1]) * 65536 + byte2Int(bytes[2]) * 256 + byte2Int(bytes[3]);
        }else if(bytes.length == 5){
            return bytes[0] * 4294967296l + byte2Int(bytes[1]) * 16777216 + byte2Int(bytes[2]) * 65536 + byte2Int(bytes[3]) * 256 +  byte2Int(bytes[4]);
        }else if(bytes.length == 6){
            return bytes[0] * 1099511627776l + byte2Int(bytes[1]) * 4294967296l + byte2Int(bytes[2]) * 16777216 + byte2Int(bytes[3]) * 65536 + byte2Int(bytes[4]) * 256 +  byte2Int(bytes[5]);
        }else if(bytes.length == 7){
            return bytes[0] * 281474976710656l + byte2Int(bytes[1]) * 1099511627776l + byte2Int(bytes[2]) * 4294967296l + byte2Int(bytes[3]) * 16777216 + byte2Int(bytes[4]) * 65536 + byte2Int(bytes[5]) * 256 +  byte2Int(bytes[6]);
        }
        return 0l;
    }

    public static int byte2Int(byte b){
        return (int)(b & 0xff);
    }

    /**
     * 通过位移方式进行计算
     * @param bs
     * @return
     */
    static long bytes2long(byte[] bs)  {
        int bytes = bs.length;

        switch(bytes) {
            case 0:
                return 0;
            case 1:
                return (long)((bs[0] & 0xff));
            case 2:
                return (long)((bs[0] & 0xff) <<8 | (bs[1] & 0xff));
            case 3:
                return (long)((bs[0] & 0xff) <<16 | (bs[1] & 0xff) <<8 | (bs[2] & 0xff));
            case 4:
                return (long)((bs[0] & 0xffL) <<24 | (bs[1] & 0xffL) << 16 | (bs[2] & 0xffL) <<8 | (bs[3] & 0xffL));
            case 5:
                return (long)((bs[0] & 0xffL) <<32 | (bs[1] & 0xffL) <<24 | (bs[2] & 0xffL) << 16 | (bs[3] & 0xffL) <<8 | (bs[4] & 0xffL));
            case 6:
                return (long)((bs[0] & 0xffL) <<40 | (bs[1] & 0xffL) <<32 | (bs[2] & 0xffL) <<24 | (bs[3] & 0xffL) << 16 | (bs[4] & 0xffL) <<8 | (bs[5] & 0xffL));
            case 7:
                return (long)((bs[0] & 0xffL) <<48 | (bs[1] & 0xffL) <<40 | (bs[2] & 0xffL) <<32 | (bs[3] & 0xffL) <<24 | (bs[4] & 0xffL) << 16 | (bs[5] & 0xffL) <<8 | (bs[6] & 0xffL));
            case 8:
                return (long)((bs[0] & 0xffL) <<56 | (bs[1] & 0xffL) << 48 | (bs[2] & 0xffL) <<40 | (bs[3] & 0xffL)<<32 |
                        (bs[4] & 0xffL) <<24 | (bs[5] & 0xffL) << 16 | (bs[6] & 0xffL) <<8 | (bs[7] & 0xffL));
            default:
                return 0;
        }
    }
}

执行结果如下:

b的值是:128
b的值是:128

b2的值是:256
b2的值是:256

b3的值是:193536
b3的值是:193536

b4的值是:16777216
b4的值是:16777216

b5的值是:4294967296
b5的值是:4294967296

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值