In practice, it is common to see two bTYE arrays combined into one, or multiple byte arrays combined into one, and int arrays converted into byte arrays, and reverse byte arrays converted into int arrays.
The following summary of the above related functions of the method, provides the corresponding tool class, directly view the source code or more tool class can pay attention to the public number “program new vision”, reply to “1005” to obtain.
Here is the code for the corresponding utility class:
package com.choupangxia.bytes; /** * Byte related public processing tools * <br/> * attention public number: program new horizon; Public class ByteUtil {/** ** Merge two byte arrays into one byte array ** @param data1 1 * @param datA2 2 * @return byte[] 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[] 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; } /** * converts an int value to a four-byte array. This method applies in the order (first byte, last byte). * * * and bytesToInt (.) @ param value to convert an 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; } /** * converts an int value to a four-byte array. This method applies in the order (first, last). Public static byte[] intToBytesHighAhead(int value) {byte[] SRC = new byte[4]; 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; } /** * int ();} /** * int (); * * @param SRC byte array * @param offset Starts at the offset bit * @return int value */ 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 array int values, this method applies to (last, first) order. * / and intToBytes2 (.) 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); }}Copy the code
Program new horizon
The public account “program new vision”, a platform for simultaneous improvement of soft power and hard technology, provides massive information
Personal public account [Program New Vision], a platform for simultaneous promotion of hard technology and soft power. Author of “Inside Spring Boo Technology: Principles of Architectural Design and Implementation” and co-author of “Inside Ethereum Smart Contract Development”. Mainly engaged in the tripartite payment industry.