Java中整形和byte数组相互转换,支持正负数

/**
     * 字节数组转int,适合转高位在前低位在后的byte[]
     * 
     * @param bytes
     * @return
     */
    public static long byteArrayToLong(byte[] bytes) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        DataInputStream dis = new DataInputStream(bais);
        long result = 0;
        try {
            int len = dis.available();
            if (len == 1) {
                result = dis.readByte();
            } else if (len == 2) {
                result = dis.readShort();
            } else if (len == 4) {
                result = dis.readInt();
            } else if (len == 8) {
                result = dis.readLong();
            }
        } catch (IOException e) {
            SyncLogUtil.e(e);
        } finally {
            try {
                dis.close();
                bais.close();
            } catch (IOException e) {
                SyncLogUtil.e(e);
            }
        }
        return result;
    }

    /**
     * int转byte[],高位在前低位在后
     * 
     * @param value
     * @return
     */
    public static byte[] varIntToByteArray(long value) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream oos = new DataOutputStream(baos);
        Long l = new Long(value);
        try {
            if (l == l.byteValue()) {
                oos.writeByte(l.byteValue());
            } else if (l == l.shortValue()) {
                oos.writeShort(l.shortValue());
            } else if (l == l.intValue()) {
                oos.writeInt(l.intValue());
            } else if (l == l.longValue()) {
                oos.writeLong(l.longValue());
            } else if (l == l.floatValue()) {
                oos.writeFloat(l.floatValue());
            } else if (l == l.doubleValue()) {
                oos.writeDouble(l.doubleValue());
            }
        } catch(IOException e) {
            SyncLogUtil.e(e);
        } finally {
            try {
                baos.close();
                oos.close();
            } catch (IOException e) {
                SyncLogUtil.e(e);
            }
        }
        return baos.toByteArray();
    }

进入DataOutputStream查看它的write方法实现改造上述方法后:

/**
     * 字节数组转int,适合转高位在前低位在后的byte[]
     * 
     * @param bytes
     * @return
     */
    public static long byteArrayToLong(byte[] bytes) {
        long result = 0;
        int len = bytes.length;
        if (len == 1) {
            byte ch = (byte) (bytes[0] & 0xff);
            result = ch;
        } else if (len == 2) {
            int ch1 = bytes[0] & 0xff;
            int ch2 = bytes[1] & 0xff;
            result = (short) ((ch1 << 8) | (ch2 << 0));
        } else if (len == 4) {
            int ch1 = bytes[0] & 0xff;
            int ch2 = bytes[1] & 0xff;
            int ch3 = bytes[2] & 0xff;
            int ch4 = bytes[3] & 0xff;
            result = (int) ((ch1 << 24) | (ch2 << 16) | (ch3 << 8) | (ch4 << 0));
        } else if (len == 8) {
            long ch1 = bytes[0] & 0xff;
            long ch2 = bytes[1] & 0xff;
            long ch3 = bytes[2] & 0xff;
            long ch4 = bytes[3] & 0xff;
            long ch5 = bytes[4] & 0xff;
            long ch6 = bytes[5] & 0xff;
            long ch7 = bytes[6] & 0xff;
            long ch8 = bytes[7] & 0xff;
            result = (ch1 << 56) | (ch2 << 48) | (ch3 << 40) | (ch4 << 32) | (ch5 << 24) | (ch6 << 16) | (ch7 << 8) | (ch8 << 0);
        }
        return result;
    }

/**
     * int转byte[],高位在前低位在后
     * 
     * @param value
     * @return
     */
    public static byte[] varIntToByteArray(long value) {
        Long l = new Long(value);
        byte[] valueBytes = null;
        if (l == l.byteValue()) {
            valueBytes = toBytes(value, 1);
        } else if (l == l.shortValue()) {
            valueBytes = toBytes(value, 2);
        } else if (l == l.intValue()) {
            valueBytes = toBytes(value, 4);
        } else if (l == l.longValue()) {
            valueBytes = toBytes(value, 8);
        }
        return valueBytes;
    }

    private static byte[] toBytes(long value, int len) {
        byte[] valueBytes = new byte[len];
        for (int i = 0;i < len;i++) {
            valueBytes[i] = (byte) (value >>> 8 * (len - i - 1));
        }
        return valueBytes;
    }

测试代码:

private static void check(long s, long r, String tag) {
        if (s == r) {
            System.out.println(tag + "[result:" + r + "]");
        } else {
            System.err.println(tag + "[source:" + s + ",result:" + r + "]");
        }
    }

    public static void main(String[] args) throws Exception {
        // byte
        byte bMax = Byte.MAX_VALUE;
        byte bMin = Byte.MIN_VALUE;
        long b_max_v = byteArrayToLong(varIntToByteArray(bMax));
        check(bMax, b_max_v, "byte");
        long b_min_v = byteArrayToLong(varIntToByteArray(bMin));
        check(bMin, b_min_v, "byte");

        // short
        short sMax = Short.MAX_VALUE;
        short sMin = Short.MIN_VALUE;
        long s_max_v = byteArrayToLong(varIntToByteArray(sMax));
        check(sMax, s_max_v, "short");
        long s_min_v = byteArrayToLong(varIntToByteArray(sMin));
        check(sMin, s_min_v, "short");

        //int
        int iMax = Integer.MAX_VALUE;
        int iMin = Integer.MIN_VALUE;
        long i_max_v = byteArrayToLong(varIntToByteArray(iMax));
        check(iMax, i_max_v, "int");
        long i_min_v = byteArrayToLong(varIntToByteArray(iMin));
        check(iMin, i_min_v, "int");

        // long
        long lMax = Long.MAX_VALUE;
        long lMin = Long.MIN_VALUE;
        long l_max_v = byteArrayToLong(varIntToByteArray(lMax));
        check(lMax, l_max_v, "long");
        long l_min_v = byteArrayToLong(varIntToByteArray(lMin));
        check(lMin, l_min_v, "long");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值