java中byte[ ]和各种数据类型的相互转换

2,329 阅读5分钟

1. int 转 byte[ ]

    /**
     * 将int转为低字节在前,高字节在后的byte数组
     */
    public static byte[] intToArrayByLow(int n) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (n & 0xff);
        bytes[1] = (byte) (n >>> 8 & 0xff);
        bytes[2] = (byte) (n >>> 16 & 0xff);
        bytes[3] = (byte) (n >>> 24 & 0xff);
        return bytes;
    }

说明:

  • & 0xff (与运算):
    1. 两个bit(1或0)进行与运算时,如果两者都为1,结果为1,否则为0,
    2. 而0xff转化为2进制为:0000000011111111,
    3. 当一个int和 0xff进行与运算时,即表示提取这个int的最低八位
      例如:
int a = 10000;// a的二进制为 10011100010000  
int b =  a & 0xff;// a和0xff 与运算后,b的二进制为 10000,即十进制的16  
System.out.println("b的十进制="+b);// 输出16

  • n >>> 8(位运算):
    >>8表示右移8位,如果该数为正,则高位补0,若为负数,则高位补1;

    例如:

1. res = 20 >> 2;   
20的二进制为 0001 0100,右移2位后为 0000 0101,则结果就为 res = 5;  
	  
2. res = -20 >> 2;  
-20的二进制为其正数的补码加1,即 11111111111111111111111111101100,  
右移2位后为 11111111111111111111111111111011,结果为 res = -5;
	  
3. 而对于>>>符号而言:  
不管正负,都是右移后高位补0;  
res = 20 >>> 2; 的结果与 20>>2 相同;  
res = -20 >>> 2;  
-20的二进制为 1110 1011,右移2位,为111010 ,此时高位补0,即 0011 1010

所以 bytes[1] = (byte) (n >>> 8 & 0xff); 表示 先右移8位,然后取最低的八位。
也即从右到左,取第9到16位


- 再特别说明一点,上面的转换方法是**采用低字节在前**的方式,一开始接触的时候看着各种协议文档写着“低字节在前”,根本不知道是什么意思。 这里解释一下低字节在前的意思,打个比方,咱们不是说int有4个字节吗? 那转成byte数组就是byte[4]对吧? byte[4]下标从0开始到3,0为前面,3为后面。 int的4个字节,最右边的是最低字节,最左边的是最高字节。 那么低字节在前的意思就是最右边的字节存在byte[0], 最左边的字节存储在byte[3]

2. byte[] 转 int

    /**
     * 低字节在前的方式获取int值
     */
    public static int getInt(byte[] b, int offset) {
        int n = 0;
        int len = b.length;
        if (len >= offset + 4) {
            // 低字节在前
            int byte0 = b[offset] & 0xff; // 最右边的字节,不需要移位
            int byte1 = b[offset + 1] & 0xff;// 右边第二个字节,需要左移一个字节,8位
            int byte2 = b[offset + 2] & 0xff;// 右边第三个字节,需要左移两个字节,16位
            int byte3 = b[offset + 3] & 0xff;// 最左边的字节,需要左移三哥字节,24位
            n = byte0 | byte1 << 8 | byte2 << 16 | byte3 << 24;
        }
        return n;
    }

上面的代码中,为什么需要对每个字节进行 & 0xff运算呢? 不懂的同学可以参考下面的链接学习一下。 blog.csdn.net/iblade/arti…

3. short转byte[]

short转成byte[]其实和 int转byte[]的逻辑一样,只不过int是四个字节,short是两个字节。

    /**
     * 将short转为低字节在前,高字节在后的byte数组
     */
    public static byte[] shortToByteArrayByLow(short n) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (n & 0xff);
        bytes[1] = (byte) (n >>> 8 & 0xff);
        return bytes;
    }

4. byte[]转short

    /**
     * @param b      低字节在前
     * @param offset
     * @return
     */
    public static short getShortByLow(byte[] b, int offset) {
        short n = 0;
        int len = b.length;
        if (len >= offset + 2) {
            // 低字节在前
            int byte0 = b[offset] & 0xff; // 最右边的字节,不需要移位
            int byte1 = b[offset + 1] & 0xff;// 右边第二个字节,需要左移一个字节,8位
            n = (short) (byte0 | byte1 << 8);
        }
        return n;
    }

5.long转byte[]

long转成byte[]其实和 int转byte[]的逻辑一样,只不过int是四个字节,long是八个字节。

    /**
     * long转byte[8],低字节在前
     */
    public static byte[] longToByteArrayByLow(long n) {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) (n & 0xff);
        bytes[1] = (byte) (n >>> 8 & 0xff);
        bytes[2] = (byte) (n >>> 16 & 0xff);
        bytes[3] = (byte) (n >>> 24 & 0xff);
        bytes[4] = (byte) (n >>> 32 & 0xff);
        bytes[5] = (byte) (n >>> 40 & 0xff);
        bytes[6] = (byte) (n >>> 48 & 0xff);
        bytes[7] = (byte) (n >>> 56 & 0xff);
        return bytes;
    }

6.byte[]转long

注意:要先把每个字节转成long类型而不是int类型哦,不然后面左移32位以上,会超出int的存储范围导致数据错误。

    /**
     * 将byte数组转为long
     */
    public static long getLongByLow(byte[] b, int offset) {
        long n = 0;
        int len = b.length;
        if (len >= offset + 8) {
            // 低字节在前
            long byte0 = b[offset] & 0xff; // 最右边的字节,不需要移位
            long byte1 = b[offset + 1] & 0xff;// 右边第二个字节,需要左移一个字节,8位
            long byte2 = b[offset + 2] & 0xff;// 右边第三个字节,需要左移两个字节,16位
            long byte3 = b[offset + 3] & 0xff;// 最左边的字节,需要左移三哥字节,24位
            long byte4 = b[offset + 4] & 0xff;// 最左边的字节,需要左移三哥字节,32位
            long byte5 = b[offset + 5] & 0xff;// 最左边的字节,需要左移三哥字节,40位
            long byte6 = b[offset + 6] & 0xff;// 最左边的字节,需要左移三哥字节,48位
            long byte7 = b[offset + 7] & 0xff;// 最左边的字节,需要左移三哥字节,56位
            n = byte0 | byte1 << 8 | byte2 << 16 | byte3 << 24 | byte4 << 32 | byte5 << 40 | byte6 << 48 | byte7 << 56;
        }
        return n;
    }