|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public class ByteUtil { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 将字节数组分割 | 
|---|
|  |  |  | * @param bytes 被分割数组 | 
|---|
|  |  |  | * @param fromIndex 分割起始位置 | 
|---|
|  |  |  | * @param len 分割长度 | 
|---|
|  |  |  | * @return 返回 分割出来的数组 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static byte[] bytesSplit(byte[] bytes, int fromIndex, int len){ | 
|---|
|  |  |  | if(bytes == null){ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | byte[] bs = new byte[len] ; | 
|---|
|  |  |  | if(fromIndex + len > bytes.length){ | 
|---|
|  |  |  | System.arraycopy(bytes, fromIndex, bs, 0, bytes.length - fromIndex) ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | System.arraycopy(bytes, fromIndex, bs, 0, len) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return bs ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 将字节数组分割 | 
|---|
|  |  |  | * @param bytes 被分割数组 | 
|---|
|  |  |  | * @param fromIndex 分割起始位置 | 
|---|
|  |  |  | * @param len 分割长度 | 
|---|
|  |  |  | * @return 返回 分割出来的数组 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Object[] bytesSplit_(byte[] bytes, int fromIndex, int len){ | 
|---|
|  |  |  | if(bytes == null){ | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | int realLen = len ; | 
|---|
|  |  |  | byte[] bs = new byte[len] ; | 
|---|
|  |  |  | if(fromIndex + len > bytes.length){ | 
|---|
|  |  |  | System.arraycopy(bytes, fromIndex, bs, 0, bytes.length - fromIndex) ; | 
|---|
|  |  |  | realLen = bytes.length - fromIndex ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | System.arraycopy(bytes, fromIndex, bs, 0, len) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new Object[]{bs, realLen} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 将字节数组合并到字节数组上 | 
|---|
|  |  |  | * @param bGroup1 被合并数组 | 
|---|
|  |  |  | 
|---|
|  |  |  | for (int i = 0; i < 4; i++) { | 
|---|
|  |  |  | bs[from + i] = Integer.valueOf(value & 0xff).byteValue();// 将最低位保存在低字节 | 
|---|
|  |  |  | value = value >> 8; // 向右移8位 | 
|---|
|  |  |  | if(value == 0){ | 
|---|
|  |  |  | break ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(value == 0){ | 
|---|
|  |  |  | break ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | throw new Exception("int2Bytes时数组越界"); | 
|---|
|  |  |  | 
|---|
|  |  |  | // s1不变 | 
|---|
|  |  |  | s0 <<= 8; | 
|---|
|  |  |  | s = s0 | s1; | 
|---|
|  |  |  | return (short) s; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return (short)s ; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | throw new Exception("byte2Short时数组越界"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return 返回 String | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String bytes2Hex(byte[] src, boolean hasBlank){ | 
|---|
|  |  |  | StringBuilder stringBuilder = new StringBuilder(""); | 
|---|
|  |  |  | if (src == null || src.length <= 0) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (int i = 0; i < src.length; i++) { | 
|---|
|  |  |  | int v = src[i] & 0xFF; | 
|---|
|  |  |  | String str = Integer.toHexString(v); | 
|---|
|  |  |  | if (str.length() < 2) { | 
|---|
|  |  |  | str = "0" + str; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (hasBlank) { | 
|---|
|  |  |  | if (i == 0) { | 
|---|
|  |  |  | stringBuilder.append(str); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | stringBuilder.append(" " + str); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | stringBuilder.append(str); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return stringBuilder.toString().toUpperCase(Locale.US); | 
|---|
|  |  |  | return bytes2Hex_BE(src, hasBlank) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 字节数组转换成十六进制的字符串 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param src byte[] | 
|---|
|  |  |  | * @param hasBlank 16进制是否用空格分隔 | 
|---|
|  |  |  | * @return 返回 String | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String bytes2Hex_BE(byte[] src, boolean hasBlank){ | 
|---|
|  |  |  | StringBuilder stringBuilder = new StringBuilder(""); | 
|---|
|  |  |  | if (src == null || src.length <= 0) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (int i = 0; i < src.length; i++) { | 
|---|
|  |  |  | int v = src[i] & 0xFF; | 
|---|
|  |  |  | String str = Integer.toHexString(v); | 
|---|
|  |  |  | if (str.length() < 2) { | 
|---|
|  |  |  | str = "0" + str; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (hasBlank) { | 
|---|
|  |  |  | if (i == 0) { | 
|---|
|  |  |  | stringBuilder.append(str); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | stringBuilder.append(" " + str); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | stringBuilder.append(str); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return stringBuilder.toString().toUpperCase(Locale.US); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 字节数组转换成十六进制的字符串 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param src byte[] | 
|---|
|  |  |  | * @param hasBlank 16进制是否用空格分隔 | 
|---|
|  |  |  | * @return 返回 String | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String bytes2Hex_LE(byte[] src, boolean hasBlank){ | 
|---|
|  |  |  | StringBuilder stringBuilder = new StringBuilder(""); | 
|---|
|  |  |  | if (src == null || src.length <= 0) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (int i = src.length - 1 ; i >= 0; i--) { | 
|---|
|  |  |  | int v = src[i] & 0xFF; | 
|---|
|  |  |  | String str = Integer.toHexString(v); | 
|---|
|  |  |  | if (str.length() < 2) { | 
|---|
|  |  |  | str = "0" + str; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (hasBlank) { | 
|---|
|  |  |  | if (i == 0) { | 
|---|
|  |  |  | stringBuilder.append(str); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | stringBuilder.append(" " + str); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | stringBuilder.append(str); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return stringBuilder.toString().toUpperCase(Locale.US); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 字节数组转换成十六进制的字符串 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param src byte[] | 
|---|
|  |  |  | * @param bs byte[] | 
|---|
|  |  |  | * @param hasBlank 16进制是否用空格分隔 | 
|---|
|  |  |  | * @param from 开始位 | 
|---|
|  |  |  | * @param len 长度 | 
|---|
|  |  |  | * @return 返回 String | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String bytes2Hex(byte[] src, boolean hasBlank, int from, int len){ | 
|---|
|  |  |  | if (src == null || src.length <= 0 || src.length < from + len) { | 
|---|
|  |  |  | public static String bytes2Hex(byte[] bs, boolean hasBlank, int from, int len){ | 
|---|
|  |  |  | if (bs == null || bs.length <= 0 || bs.length < from + len) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | byte[] bb = new byte[len]; | 
|---|
|  |  |  | for (int i = 0 ; i < len; i++) { | 
|---|
|  |  |  | bb[i] = src[from + i]; | 
|---|
|  |  |  | bb[i] = bs[from + i]; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return bytes2Hex(bb, hasBlank) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 字节数组转换成十六进制的字符串 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param bs byte[] | 
|---|
|  |  |  | * @param hasBlank 16进制是否用空格分隔 | 
|---|
|  |  |  | * @param from 开始位 | 
|---|
|  |  |  | * @param len 长度 | 
|---|
|  |  |  | * @return 返回 String | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String bytes2Hex_LE(byte[] bs, boolean hasBlank, int from, int len){ | 
|---|
|  |  |  | if (bs == null || bs.length <= 0 || bs.length < from + len) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | byte[] bb = new byte[len]; | 
|---|
|  |  |  | for (int i = 0 ; i < len; i++) { | 
|---|
|  |  |  | bb[i] = bs[from + i]; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return bytes2Hex_LE(bb, hasBlank) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 十六进制转字节数组 | 
|---|
|  |  |  | * @param hex the hex string | 
|---|
|  |  |  | 
|---|
|  |  |  | return fromIndex ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 将byte[]转换为16进制字符串 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param bytes 待转换byte[] | 
|---|
|  |  |  | * @return 返回 转换后的字符串 | 
|---|
|  |  |  | * 十六进制转字节数组 | 
|---|
|  |  |  | * @param hex the hex string | 
|---|
|  |  |  | * @return 返回 byte[] | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String bytesToHex(byte[] bytes) { | 
|---|
|  |  |  | //一个byte为8位,可用两个十六进制位标识 | 
|---|
|  |  |  | char[] buf = new char[bytes.length * 2]; | 
|---|
|  |  |  | int a = 0; | 
|---|
|  |  |  | int index = 0; | 
|---|
|  |  |  | for (byte b : bytes) { // 使用除与取余进行转换 | 
|---|
|  |  |  | if (b < 0) { | 
|---|
|  |  |  | a = 256 + b; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | a = b; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | buf[index++] = HEX_CHAR[a / 16]; | 
|---|
|  |  |  | buf[index++] = HEX_CHAR[a % 16]; | 
|---|
|  |  |  | public static int hex2Bytes_LE(String hex, byte[] bs, int fromIndex) { | 
|---|
|  |  |  | if (hex == null || hex.equals("")) { | 
|---|
|  |  |  | return fromIndex; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new String(buf); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 将byte[]转换为16进制字符串 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param bytes 待转换byte[] | 
|---|
|  |  |  | * @return 返回 转换后的字符串 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String bytesToHex(byte[] bytes, int startIndex, int endIndex) { | 
|---|
|  |  |  | byte[] bs = new byte[endIndex - startIndex + 1] ; | 
|---|
|  |  |  | byte j = 0 ; | 
|---|
|  |  |  | for(int i = startIndex; i <= endIndex; i++){ | 
|---|
|  |  |  | bs[j++] = bytes[i] ; | 
|---|
|  |  |  | hex = hex.toUpperCase(Locale.ENGLISH); | 
|---|
|  |  |  | int length = hex.length() / 2; | 
|---|
|  |  |  | char[] hexChars = hex.toCharArray(); | 
|---|
|  |  |  | byte[] d = new byte[length]; | 
|---|
|  |  |  | for (int i = 0; i < length; i++) { | 
|---|
|  |  |  | int pos = i * 2; | 
|---|
|  |  |  | d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //一个byte为8位,可用两个十六进制位标识 | 
|---|
|  |  |  | char[] buf = new char[bs.length * 2]; | 
|---|
|  |  |  | int a = 0; | 
|---|
|  |  |  | int index = 0; | 
|---|
|  |  |  | for (byte b : bs) { // 使用除与取余进行转换 | 
|---|
|  |  |  | if (b < 0) { | 
|---|
|  |  |  | a = 256 + b; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | a = b; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | buf[index++] = HEX_CHAR[a / 16]; | 
|---|
|  |  |  | buf[index++] = HEX_CHAR[a % 16]; | 
|---|
|  |  |  | for(int i = 0 ; i < d.length; i++){ | 
|---|
|  |  |  | bs[fromIndex++] = d[(d.length - 1) - i] ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new String(buf); | 
|---|
|  |  |  | return fromIndex ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 将16进制字符串转换为byte[] | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param str 待转换字符串 | 
|---|
|  |  |  | * @return 返回 转换后的byte[] | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static byte[] hexToBytes(String str) { | 
|---|
|  |  |  | if (str == null || "".equals(str.trim())) { | 
|---|
|  |  |  | return new byte[0]; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | byte[] bytes = new byte[str.length() / 2]; | 
|---|
|  |  |  | for (int i = 0; i < str.length() / 2; i++) { | 
|---|
|  |  |  | String subStr = str.substring(i * 2, i * 2 + 2); | 
|---|
|  |  |  | bytes[i] = (byte) Integer.parseInt(subStr, 16); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return bytes; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Convert char to byte | 
|---|
|  |  |  | 
|---|
|  |  |  | encodeBCD_LE(str, b, 0, b.length); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return b; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 小端模式《数据低位在数组低字节》 | 
|---|
|  |  |  | * 整形转成BCD编码,字节顺序是倒的 | 
|---|
|  |  |  | * @param i | 
|---|
|  |  |  | * @param bs | 
|---|
|  |  |  | * @param from | 
|---|
|  |  |  | * @return 返回 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static void int2BCD_LE(int i, byte[] bs, int from)throws Exception { | 
|---|
|  |  |  | String str = "" + i; | 
|---|
|  |  |  | byte[] b = null; | 
|---|
|  |  |  | if (str.length() % 2 == 0) { | 
|---|
|  |  |  | b = new byte[str.length() / 2]; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | b = new byte[(str.length() / 2) + 1]; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | encodeBCD_LE(str, b, 0, b.length); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int len = bs.length ; | 
|---|
|  |  |  | int bLen = b.length ; | 
|---|
|  |  |  | for(int j = 0; (j < len && j < bLen); j++){ | 
|---|
|  |  |  | bs[from + j] = b[j] ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | public static long BCD2Long_LE(byte[] b, int startIndex, int endIndex)throws Exception { | 
|---|
|  |  |  | String str = ""; | 
|---|
|  |  |  | str = decodeBCD_LE(b, startIndex, endIndex - startIndex + 1); | 
|---|
|  |  |  | long n = Long.parseLong(str); | 
|---|
|  |  |  | return n; | 
|---|
|  |  |  | return Long.parseLong(str); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|