Common tool-like methods for processing bytes are for your reference. The specific content is as follows
package com.demo.utils;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.nio.charset.Charset;import java.util.Arrays;/** * Common tool class methods for handling bytes* @author dongyangyang * @Date 2017/3/13 12:31 * @Version 1.0 * */public class ByteUtils { /** * The value table that needs to be associated with when constructing a new byte*/ private static final byte[] BUILD_BYTE_TABLE = new byte[] { (byte) 128, (byte) 64, (byte) 32, (byte) 16, (byte) 8, (byte) 4, (byte) 2, (byte) 1 }; private ByteUtils() {} /** * short convert to byte array* * @param number * Data to be converted. * @return Converted byte array. */ public static byte[] shortToByte(short number) { byte[] b = new byte[2]; for (int i = 1; i >= 0; i--) { b[i] = (byte) (number % 256); number >>= 8; } return b; } /** * Byte to short conversion* * @param b * short byte array* @return short value. */ public static short byteToShort(byte[] b) { return (short) (((((b[0] & 0xff) << 8) | b[1] & 0xff)); } /** * Integer conversion to byte array* * @param number * Skew data. * @return Byte array of shaping data. */ public static byte[] intToByte(int number) { byte[] b = new byte[4]; for (int i = 3; i >= 0; i--) { b[i] = (byte) (number % 256); number >>= 8; } return b; } /** * Byte array to integer conversion* * @param b * Byte array of shaping data. * @return The shaping data converted into byte array. */ public static int byteToInt(byte[] b) { return (((((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | (b[3] & 0xff))); } /** * long convert to byte array* * @param number * Long skew data. * @return A byte array converted to long shaping. */ public static byte[] longToByte(long number) { byte[] b = new byte[8]; for (int i = 7; i >= 0; i--) { b[i] = (byte) (number % 256); number >>= 8; } return b; } /** * Conversion from byte array to integer* * @param b * Long-shaped byte array. * @return Long Surgery Data. */ public static long byteToLong(byte[] b) { return ((((long) b[0] & 0xff) << 56) | (((long) b[1] & 0xff) << 48) | (((long) b[2] & 0xff) << 40) | (((long) b[3] & 0xff) << 32) | (((long) b[4] & 0xff) << 24) | (((long) b[5] & 0xff) << 16) | (((long) b[6] & 0xff) << 8) | (((long) b[7] & 0xff)); } /** * double conversion to byte array* * @param d * Double precision floating point. * @return byte array of double precision floating point. */ public static byte[] doubleToByte(double d) { byte[] bytes = new byte[8]; long l = Double.doubleToLongBits(d); for (int i = 0; i < bytes.length; i++) { bytes[i] = Long.valueOf(l).byteValue(); l = l >> 8; } return bytes; } /** * Byte array to double conversion* * @param b * Double precision floating point byte array. * @return Double precision floating point data. */ public static double byteToDouble(byte[] b) { long l; l = b[0]; l &= 0xff; l |= ((long) b[1] << 8); l &= 0xffffff; l |= ((long) b[2] << 16); l &= 0xffffffff; l |= ((long) b[3] << 24); l &= 0xffffffffl; l |= ((long) b[4] << 32); l &= 0xffffffffffl; l |= ((long) b[5] << 40); l &= 0xffffffffffffl; l |= ((long) b[6] << 48); l &= 0xffffffffffffffffl; l |= ((long) b[7] << 56); return Double.longBitsToDouble(l); } /** * float converts to byte array* * @param d * floating point data. * @return A byte array converted bytes from floating point data. */ public static byte[] floatToByte(float d) { byte[] bytes = new byte[4]; int l = Float.floatToIntBits(d); for (int i = 0; i < bytes.length; i++) { bytes[i] = Integer.valueOf(l).byteValue(); l = l >> 8; } return bytes; } /** * Conversion from byte array to float* * @param b * Float-point data byte array. * @return Floating point data. */ public static float byteToFloat(byte[] b) { int l; l = b[0]; l &= 0xff; l |= ((long) b[1] << 8); l &= 0xffffff; l |= ((long) b[2] << 16); l &= 0xffffffff; l |= ((long) b[3] << 24); l &= 0xffffffffl; return Float.intBitsToFloat(l); } /** * String to byte array conversion* * @param s * String. * @param charset * Character encoding* @return The byte array encoded by the string according to the corresponding character. */ public static byte[] stringToByte(String s, Charset charset) { return s.getBytes(charset); } /** * Conversion of byte array with string* * @param b * Byte array converted by strings by specified encoding. * @param charset * Character encoding. * @return string. */ public static String byteToString(byte[] b, Charset charset) { return new String(b, charset); } /** * Object is converted into a byte array. * * @param obj * byte array. * @return The corresponding serialized byte array of object instances. * @throws IOException */ public static byte[] objectToByte(Object obj) throws IOException { ByteArrayOutputStream buff = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(buff); out.writeObject(obj); try { return buff.toByteArray(); } finally { out.close(); } } /** * Sequentially dead byte array converted into actual objects. * * @param b * byte array. * @return object. * @throws IOException * @throws ClassNotFoundException */ public static Object byteToObject(byte[] b) throws IOException, ClassNotFoundException { ByteArrayInputStream buff = new ByteArrayInputStream(b); ObjectInputStream in = new ObjectInputStream(buff); Object obj = in.readObject(); try { return obj; } finally { in.close(); } } /** * Compare whether each bit of the two bytes is equal. * * @param a * Compared byte. * @param b * Compared byte* @return truth Each bit of the two bytes is equal, false has at least one bit that is not equal. */ public static boolean equalsBit(byte a, byte b) { return Arrays.equals(byteToBitArray(a), byteToBitArray(b)); } /** * Comparison of each byte in two arrays. The two bytes must be the same in binary byte code to indicate that the two bytes are the same. * * @param a * Compared byte array. * @param b * The number of bytes to be compared. * @return truth Each bit of each element and two arrays are equal, and false have at least one bit of it not equal. */ public static boolean equalsBit(byte[] a, byte[] b) { if (a == b) { return true; } if (a == null || b == null) { return false; } int length = a.length; if (b.length != length) { return false; } for (int count = 0; count < a.length; count++) { if (!equalsBit(a[count], b[count])) { return false; } } return true; } /** * Return a string composed of bits of a byte. * * @param b * byte. * @return A string composed of Bit bits. */ public static String bitString(byte b) { StringBuilder buff = new StringBuilder(); boolean[] array = byteToBitArray(b); for (int i = 0; i < array.length; i++) { buff.append(array[i] ? 1 : 0); } return buff.toString(); } /** * Calculate each bit in a given byte and return it as a boolean array. true is expressed as 1, false is expressed as 0. * * @param b * byte. * @return An array composed of each bit bit of the specified byte. */ public static boolean[] byteToBitArray(byte b) { boolean[] buff = new boolean[8]; int index = 0; for (int i = 7; i >= 0; i--) { buff[index++] = ((b >>> i) & 1) == 1; } return buff; } /** * Return the specified bit bit in the specified byte, true is 1, false is 0. The specified bit is from 0-7, exceeding the data out-of-bounds exception to be thrown. * * @param b * Byte to be judged. * @param index * Specified bit in byte. * @return The value of the bit is specified. */ public static boolean byteBitValue(byte b, int index) { return byteToBitArray(b)[index]; } /** * Construct a new byte based on the binary represented by the boolean array. * * @param values * Boolean array, where true is represented as 1 and false is represented as 0. * @return The new byte constructed. */ public static byte buildNewByte(boolean[] values) { byte b = 0; for (int i = 0; i < 8; i++) { if (values[i]) { b |= BUILD_BYTE_TABLE[i]; } } return b; } /** * Replace a bit bit in the specified byte with the specified value, true represents 1, false represents 0. * * @param b * Byte to be replaced. * @param index * The number of bits, starting from 0. Over 7 will throw an out-of-bounds exception. * @param newValue * New value. * @return Replace a new byte of a bit value. */ public static byte changeByteBitValue(byte b, int index, boolean newValue) { boolean[] bitValues = byteToBitArray(b); bitValues[index] = newValue; return buildNewByte(bitValues); } /** * Convert the specified IP address into a byte representation. Each number in the IP array cannot be greater than 255, otherwise an IllegalArgumentException exception will be thrown. * * @param ipNums * IP address array. * @return IP address byte representation. */ public static byte[] ipAddressBytes(String address) { if (address == null || address.length() < 0 || address.length() > 15) { throw new IllegalArgumentException("Invalid IP address."); } final int ipSize = 4;// Maximum IP digits final char ipSpace = '.';// The delimiter of IP numbers int[] ipNums = new int[ipSize]; StringBuilder number = new StringBuilder();// The current number of operation StringBuilder buff = new StringBuilder(address); int point = 0;// The number subscript of the current operation is up to 3. char currentChar; for (int i = 0; i < buff.length(); i++) { currentChar = buff.charAt(i); if (ipSpace == currentChar) { // After the current position is equal to the maximum number, there are still characters that are not processed to indicate that this is an incorrect IP. if (point == ipSize - 1 && buff.length() - (i + 1) > 0) { throw new IllegalArgumentException("Invalid IP address."); } ipNums[point++] = Integer.parseInt(number.toString()); number.delete(0, number.length()); } else { number.append(currentChar); } } ipNums[point] = Integer.parseInt(number.toString()); byte[] ipBuff = new byte[ipSize]; int pointNum = 0; for (int i = 0; i < 4; i++) { pointNum = Math.abs(ipNums[i]); if (pointNum > 255) { throw new IllegalArgumentException("Invalid IP address."); } ipBuff[i] = (byte) (pointNum & 0xff); } return ipBuff; }} The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.