编程知识 cdmana.com

Merging two or more byte arrays and converting int type to array in Java

In practice, we often encounter two btye The arrays are merged into one , Or more byte The arrays are merged into one , as well as int Type transfer byte Array , Converse byte Array rotation int type .

The following summarizes the methods of the above related functions , The corresponding tool classes are provided , You can look at source code or more tools and you can pay attention to the official account “ New perspective of procedure ”, reply “1005” get .

The following is the code of the corresponding tool class :

package com.choupangxia.bytes;

/**
 * Byte Related public processing tools 
 * <br/>
 *  Official account : New perspective of procedure ; reply "1005" Get more tool classes 
 *
 * @author sec
 * @version 1.0
 * @date 2020/12/22
 **/
public class ByteUtil {
    

    /**
     *  Merge two byte arrays into one byte array 
     *
     * @param data1  Byte array 1
     * @param data2  Byte array 2
     * @return byte[]  The combined byte number 
     */
    public static byte[] mergeBytes(byte[] data1, byte[] data2) {
    
        byte[] result = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, result, 0, data1.length);
        System.arraycopy(data2, 0, result, data1.length, data2.length);
        return result;
    }

    /**
     *  Merge multiple byte arrays into one byte array 
     *
     * @param values  Dynamic byte number parameter 
     * @return byte[]  The combined byte number 
     */
    private static byte[] mergeBytes(byte[]... values) {
    
        int lengthByte = 0;
        for (byte[] value : values) {
    
            lengthByte += value.length;
        }
        byte[] allBytes = new byte[lengthByte];
        int countLength = 0;
        for (byte[] b : values) {
    
            System.arraycopy(b, 0, allBytes, countLength, b.length);
            countLength += b.length;
        }
        return allBytes;
    }

    /**
     *  take int The number is converted to four bytes byte Array , This method is applicable to ( Little Endian , High position behind ) The order of .
     *  and bytesToInt() Matching use of 
     *
     * @param value  To convert int value 
     * @return byte Array 
     */
    public static byte[] intToBytesLowAhead(int value) {
    
        byte[] src = new byte[4];
        src[3] = (byte) ((value >> 24) & 0xFF);
        src[2] = (byte) ((value >> 16) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[0] = (byte) (value & 0xFF);
        return src;
    }

    /**
     *  take int The number is converted to four bytes byte Array , This method is applicable to ( High in the former , Low in the back ) The order of .
     *  and bytesToInt2() Matching use of 
     */
    public static byte[] intToBytesHighAhead(int value) {
    
        byte[] src = new byte[4];
        src[0] = (byte) ((value >> 24) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }

    /**
     * byte From array int The number , This method is applicable to ( Little Endian , High position behind ) The order of , And intToBytes() Matching use of 
     *
     * @param src    byte Array 
     * @param offset  From the first... Of the array offset Bit start 
     * @return int The number 
     */
    public static int bytesToIntLowAhead(byte[] src, int offset) {
    
        return (src[offset] & 0xFF)
                | ((src[offset + 1] & 0xFF) << 8)
                | ((src[offset + 2] & 0xFF) << 16)
                | ((src[offset + 3] & 0xFF) << 24);
    }

    /**
     * byte From array int The number , This method is applicable to ( Low in the back , High in the former ) The order of . and intToBytes2() Matching use of 
     */
    public static int bytesToIntHighAhead(byte[] src, int offset) {
    
        return ((src[offset] & 0xFF) << 24)
                | ((src[offset + 1] & 0xFF) << 16)
                | ((src[offset + 2] & 0xFF) << 8)
                | (src[offset + 3] & 0xFF);
    }
}

New perspective of procedure

official account “ New perspective of procedure ”, One makes you soft power 、 A platform for synchronous upgrading of hardware technology , Provide massive data

 WeChat official account : New perspective of procedure

版权声明
本文为[New vision of procedure]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224224101550n.html

Scroll to Top