3.byte,long,double,int,之间互转
/** * int转byte[],四个字节 * @param value * @return */ public static byte[] intToBytes(int value) { byte[] src = new byte[4]; if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) { for (int i = 0; i < src.length; i++) { src[i] = (byte) ((value >> (8 * i)) & 0xFF); } } else { for (int i = 0; i < src.length; i++) { src[i] = (byte) ((value >> (8 * (src.length - 1 - i))) & 0xFF); } } return src; } /** * byte[]转int * @param src * @return */ public static int bytesToInt(byte[] src) { return bytesToInt(src, 0, 4); } /** * byte[]中指定位置转int * @param src * @param start 起始位置,含 * @param end 模式位置,不含 * @return */ public static int bytesToInt(byte[] src, int start, int end) { int retVal = 0; if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) { for (int i = start; i < end; i++) { retVal |= (((src[i] & 0xFF) << (8 * i))); } } else { for (int i = start; i < end; i++) { retVal |= (((src[i] & 0xFF) << (8 * (end - 1 - i)))); } } return retVal; } /** * long转byte[] * @param value * @return */ public static byte[] longToBytes(long value) { byte[] src = new byte[8]; if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) { for (int i = 0; i < src.length; i++) { src[i] = (byte) ((value >> (8 * i)) & 0xFF); } } else { for (int i = 0; i < src.length; i++) { src[i] = (byte) ((value >> (8 * (src.length - 1 - i))) & 0xFF); } } return src; } /** * byte[]中指定位置转long * @param src * @param start 起始位置,含 * @param end 模式位置,不含 * @return */ public static long bytesToLong(byte[] src, int start, int end) { long retVal = 0; if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) { for (int i = start; i < end; i++) { retVal |= (((src[i] & 0xFF) << (8 * i))); } } else { for (int i = start; i < end; i++) { retVal |= (((src[i] & 0xFF) << (8 * (end - 1 - i)))); } } return retVal; } /** * byte[]转long * @param src * @return */ public static long bytesToLong(byte[] src) { return bytesToLong(src, 0, 8); } /** * 将几个byte[]组合成新的byte[] * @param arrs * @return */ public static byte[] addByteArr(byte[]... arrs) { int len = 0; for (byte[] byteOnes : arrs) { len += byteOnes.length; } byte[] retVal = new byte[len]; int offset = 0; for (byte[] byteTwos : arrs) { System.arraycopy(byteTwos, 0, retVal, offset, byteTwos.length); offset += byteTwos.length; } return retVal; } /** * byte[]转double * @param bytes * @return */ public static double getDouble(byte[] bytes) { long l = getLong(bytes); return Double.longBitsToDouble(l); } /** * double转byte[] * @param data * @return */ public static byte[] getBytes(double data) { long intBits = Double.doubleToLongBits(data); return getBytes(intBits); } /** * 将bytes[]转成long * @param bytes * @return */ public static long getLong(byte[] bytes) { return (0xffL & (long) bytes[0]) | (0xff00L & ((long) bytes[1] << 8)) | (0xff0000L & ((long) bytes[2] << 16)) | (0xff000000L & ((long) bytes[3] << 24)) | (0xff00000000L & ((long) bytes[4] << 32)) | (0xff0000000000L & ((long) bytes[5] << 40)) | (0xff000000000000L & ((long) bytes[6] << 48)) | (0xff00000000000000L & ((long) bytes[7] << 56)); } /** * long转byte[] * @param data * @return */ public static byte[] getBytes(long data) { byte[] bytes = new byte[8]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data >> 8) & 0xff); bytes[2] = (byte) ((data >> 16) & 0xff); bytes[3] = (byte) ((data >> 24) & 0xff); bytes[4] = (byte) ((data >> 32) & 0xff); bytes[5] = (byte) ((data >> 40) & 0xff); bytes[6] = (byte) ((data >> 48) & 0xff); bytes[7] = (byte) ((data >> 56) & 0xff); return bytes; }
/** * Encodes an array of bytes as String representation of hexadecimal. * @param bytes an array of bytes to convert to a hex string. * @return generated hex string. */ public static String encodeHex(byte[] bytes) { StringBuilder hex = new StringBuilder(bytes.length * 2); for (byte aByte : bytes) { if ((aByte & 0xff) < 0x10) { hex.append("0"); } hex.append(Integer.toString(aByte & 0xff, 16)); } return hex.toString(); }