有符号、无符号的short、int、long与byte之间的转换

short、int、long转byte

 有符号的类型转换:

 //short类型转换byte[]
    public byte[] shortToByteArray(short s, boolean littleEndian) {
        //littleEndian:true 高位在前 false 低位在前
        byte[] bytes = new byte[2];
        for (int i = 0; i < 2; i++) {
            int i1 = (littleEndian ? i : (1 - i)) << 3;
            //int i1 = (littleEndian?i:(1-i))*8;
            bytes[i] = (byte) ((s >> i1) & 0xff);
        }
        return bytes;
    }

    //int类型转换byte[]
    public byte[] IntToByteArray(long l, boolean littleEndian) {
        byte[] bytes = new byte[4];
        for (int i = 0; i < 4; i++) {
            int i1 = (littleEndian ? i : (3 - i)) << 3;
            bytes[i] = (byte) ((l >> i1) & 0xff);
        }
        return bytes;
    }

    //long类型转换byte[]
    public byte[] longToByteArray(long l, boolean littleEndian) {
        byte[] bytes = new byte[8];
        for (int i = 0; i < 8; i++) {
            int i1 = (littleEndian ? i : (7 - i)) << 3;
            bytes[i] = (byte) ((l >> i1) & 0xff);
        }
        return bytes;
    }
无符号的类型转换:

//int类型转换byte[]
    public byte[] unIntToByteArray(long l, boolean littleEndian) {
        byte[] bytes = new byte[2];
        for (int i = 0; i < 2; i++) {
            int i1 = (littleEndian ? i : (1 - i)) << 3;
            bytes[i] = (byte) ((l >> i1) & 0xff);
        }
        return bytes;
    }

    //long类型转换byte[]
    public byte[] unlongToByteArray(long l, boolean littleEndian) {
        byte[] bytes = new byte[4];
        for (int i = 0; i < 4; i++) {
            int i1 = (littleEndian ? i : (3 - i)) << 3;
            bytes[i] = (byte) ((l >> i1) & 0xff);
        }
        return bytes;
    }

 

有符号的类型转换://偏移位置,为偏移传0

    //byte转long
    public static long longFrom8Bytes(byte[] input, int offset, boolean littleEndian) {
        long value = 0; // 循环读取每个字节通过移位运算完成long的4个字节拼装
        for (int count = 0; count < 8; ++count) {
            int shift = (littleEndian ? count : (7 - count)) << 3;
            value |= ((long) 0xff << shift) & ((long) input[offset + count] << shift);
        }
        return value;
    }

    //byte转int
    public static int intFrom4Bytes(byte[] input, int offset, boolean littleEndian) {
        int value = 0;
        // 循环读取每个字节通过移位运算完成long的8个字节拼装
        for (int count = 0; count < 4; ++count) {
            int shift = (littleEndian ? count : (3 - count)) << 3;
            value |= ((long) 0xff << shift) & ((long) input[offset + count] << shift);
        }
        return value;
    }

    //byte转short
    public static int shortFrom2Bytes(byte[] input, int offset, boolean littleEndian) {
        int value = 0;
        // 循环读取每个字节通过移位运算完成long的8个字节拼装
        for (int count = 0; count < 2; ++count) {
            int shift = (littleEndian ? count : (1 - count)) << 3;
            value |= ((long) 0xff << shift) & ((long) input[offset + count] << shift);
        }
        return value;
    }
 

 

 

无符号的类型转换:
   //byte转long
    public static long unlongFrom4Bytes(byte[] input, int offset, boolean littleEndian) {
        long value = 0; // 循环读取每个字节通过移位运算完成long的4个字节拼装
        for (int count = 0; count < 4; ++count) {
            int shift = (littleEndian ? count : (3 - count)) << 3;
            value |= ((long) 0xff << shift) & ((long) input[offset + count] << shift);
        }
        return value;
    }

    //byte转int
    public static int unintFrom2Bytes(byte[] input, int offset, boolean littleEndian) {
        int value = 0;
        // 循环读取每个字节通过移位运算完成long的8个字节拼装
        for (int count = 0; count < 2; ++count) {
            int shift = (littleEndian ? count : (1 - count)) << 3;
            value |= ((long) 0xff << shift) & ((long) input[offset + count] << shift);
        }
        return value;
    }
 

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值