| | |
| | | * @param index 下标位 |
| | | * @param len 长度 |
| | | * @return 返回 返回 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static boolean bytesIsAll0xFF(byte[] bs, int index, int len)throws Exception { |
| | | public static boolean bytesIsAll0xFF(byte[] bs, int index, int len){ |
| | | int count = 0 ; |
| | | for(int i = index; i < index + len; i++){ |
| | | if(bs[i] == (byte)0xFF){ |
| | |
| | | |
| | | /** |
| | | * 二进制转十进制数 |
| | | * @param str |
| | | * @param str 二进制字符串 |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static int binary2Int(String str) throws Exception { |
| | | int cnt=0; |
| | | int sum=0; |
| | | str=new StringBuilder(str).reverse().toString();//反转字符串 |
| | | for(int i=0;i<str.length();i++){ |
| | | cnt++; |
| | | if (str.charAt(i)=='1'){ |
| | | int mul=1; |
| | | for (int j=1;j<cnt;j++){ |
| | | mul*=2; |
| | | } |
| | | sum+=mul; |
| | | } |
| | | else continue; |
| | | } |
| | | return sum; |
| | | public static int binary2Int(String str) { |
| | | return Integer.parseInt(str, 2); |
| | | } |
| | | |
| | | |
| | |
| | | * 字节转存二进制 |
| | | * |
| | | * @param b byte |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | | */ |
| | | public static String byte2Binary(byte b) throws Exception { |
| | | int n = (b + 256) % 256 + 256; |
| | | try { |
| | | return Integer.toBinaryString(n).substring(1); |
| | | } catch (Exception e) { |
| | | throw new Exception("字节转换成二进制的字符串出错!", null); |
| | | } |
| | | public static String byte2Binary(byte b) { |
| | | return Integer.toBinaryString(b & 0xFF) ; |
| | | } |
| | | /** |
| | | * 字节转存8位二进制 |
| | | * |
| | | * @param b |
| | | * byte |
| | | * @throws Exception 异常 |
| | | * @param b byte |
| | | * @return 返回 String |
| | | */ |
| | | public static String byte2bit8Binary(byte b) throws Exception { |
| | | public static String byte2bit8Binary(byte b) { |
| | | String s = byte2Binary(b); |
| | | int len = s.length(); |
| | | for (int i = 0; i < 8 - len; i++) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 字节取bit |
| | | * @param b |
| | | * @param b |
| | | * @throws Exception 异常 |
| | | * 字节数 取出8个bit |
| | | * @param b 字节数 |
| | | * @return 返回 String |
| | | */ |
| | | public static byte[] getBit(byte b) throws Exception { |
| | | public static byte[] getBit(byte b) { |
| | | byte[] bs = new byte[8] ; |
| | | bs[0] = (byte)(b & 1) ; |
| | | bs[1] = (byte)((b & 2) >> 1) ; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 字节取bit |
| | | * @param b |
| | | * 字节数 取出bit |
| | | * @param b 字节数 |
| | | * @param index 下标位 |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | |
| | | public static void double2Bytes_BE(byte[] bs, double value, int from)throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | | if (b) { |
| | | Long l = Double.doubleToLongBits(value); |
| | | long2Bytes_BE(bs, l, from); |
| | | long2Bytes_BE(bs, Double.doubleToLongBits(value), from); |
| | | } else { |
| | | throw new Exception("double2Bytes时数组越界"); |
| | | } |
| | |
| | | public static void double2Bytes_LE(byte[] bs, double value, int from)throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | | if (b) { |
| | | Long l = Double.doubleToLongBits(value); |
| | | long2Bytes_LE(bs, l, from); |
| | | long2Bytes_LE(bs, Double.doubleToLongBits(value), from); |
| | | } else { |
| | | throw new Exception("double2Bytes时数组越界"); |
| | | } |
| | |
| | | long l = 0; |
| | | long[] ls = new long[len] ; |
| | | for(int i = 0 ; i < len; i++){ |
| | | ls[i] = bs[i] ; |
| | | ls[i] = bs[i] & 0xFF ; |
| | | } |
| | | for(int i = len-1 ; i >= 0; i--){ |
| | | ls[(len - 1) - i] <<= 8 * i ; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | | if (b) { |
| | | long s = 0; |
| | | long s0 = bs[from + 0] ;// 最低位 |
| | | long s1 = bs[from + 1] ; |
| | | long s2 = bs[from + 2] ; |
| | | long s3 = bs[from + 3] ; |
| | | long s4 = bs[from + 4] ; |
| | | long s5 = bs[from + 5] ; |
| | | long s6 = bs[from + 6] ; |
| | | long s7 = bs[from + 7] ; |
| | | long s0 = bs[from + 0] & 0xFF ;// 最低位 |
| | | long s1 = bs[from + 1] & 0xFF ; |
| | | long s2 = bs[from + 2] & 0xFF ; |
| | | long s3 = bs[from + 3] & 0xFF ; |
| | | long s4 = bs[from + 4] & 0xFF ; |
| | | long s5 = bs[from + 5] & 0xFF ; |
| | | long s6 = bs[from + 6] & 0xFF ; |
| | | long s7 = bs[from + 7] & 0xFF ; |
| | | |
| | | // s7不变 |
| | | s6 <<= 8; |
| | |
| | | short len = (short)(end - from + 1) ; |
| | | long[] ls = new long[len] ; |
| | | for(short i = 0 ; i < len; i++){ |
| | | ls[i] = bs[from + i] ; |
| | | ls[i] = bs[from + i] & 0xFF ; |
| | | } |
| | | for(short i = (short)(len-1) ; i >= 0; i--){ |
| | | ls[i] <<= 8 * (len - (i + 1)) ; |
| | |
| | | long l = 0; |
| | | long[] ls = new long[len] ; |
| | | for(int i = 0 ; i < len; i++){ |
| | | ls[i] = bs[i] ; |
| | | ls[i] = bs[i] & 0xFF ; |
| | | } |
| | | for(int i = 0 ; i < len; i++){ |
| | | ls[(len - 1) - i] <<= 8 * i ; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | | if (b) { |
| | | long s = 0; |
| | | long s0 = bs[from + 0] ;// 最低位 |
| | | long s1 = bs[from + 1] ; |
| | | long s2 = bs[from + 2] ; |
| | | long s3 = bs[from + 3] ; |
| | | long s4 = bs[from + 4] ; |
| | | long s5 = bs[from + 5] ; |
| | | long s6 = bs[from + 6] ; |
| | | long s7 = bs[from + 7] ; |
| | | long s0 = bs[from] & 0xFF ;// 最低位 |
| | | long s1 = bs[from + 1] & 0xFF ; |
| | | long s2 = bs[from + 2] & 0xFF ; |
| | | long s3 = bs[from + 3] & 0xFF ; |
| | | long s4 = bs[from + 4] & 0xFF ; |
| | | long s5 = bs[from + 5] & 0xFF ; |
| | | long s6 = bs[from + 6] & 0xFF ; |
| | | long s7 = bs[from + 7] & 0xFF ; |
| | | |
| | | // s0不变 |
| | | s1 <<= 8; |
| | |
| | | short len = (short)(end - from + 1) ; |
| | | long[] ls = new long[len] ; |
| | | for(short i = 0 ; i < len; i++){ |
| | | ls[i] = bs[from + i] ; |
| | | ls[i] = bs[from + i] & 0xFF ; |
| | | } |
| | | for(short i = 0 ; i < len; i++){ |
| | | ls[i] <<= 8 * i ; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); |
| | | if (b) { |
| | | int s = 0; |
| | | int s0 = bs[from + 0] ;// 最低位 |
| | | int s1 = bs[from + 1] ; |
| | | int s2 = bs[from + 2] ; |
| | | int s3 = bs[from + 3] ; |
| | | int s0 = bs[from + 0] & 0xFF ;// 最低位 |
| | | int s1 = bs[from + 1] & 0xFF ; |
| | | int s2 = bs[from + 2] & 0xFF ; |
| | | int s3 = bs[from + 3] & 0xFF ; |
| | | |
| | | // s3不变 |
| | | s2 <<= 8; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); |
| | | if (b) { |
| | | int s = 0; |
| | | int s0 = bs[from + 0] ;// 最低位 |
| | | int s1 = bs[from + 1] ; |
| | | int s2 = bs[from + 2] ; |
| | | int s3 = bs[from + 3] ; |
| | | int s0 = bs[from + 0] & 0xFF ;// 最低位 |
| | | int s1 = bs[from + 1] & 0xFF ; |
| | | int s2 = bs[from + 2] & 0xFF ; |
| | | int s3 = bs[from + 3] & 0xFF ; |
| | | |
| | | // s0不变 |
| | | s1 <<= 8; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); |
| | | if (b) { |
| | | int s = 0; |
| | | int s0 = bs[from + 0] ; |
| | | int s1 = bs[from + 1] ; |
| | | int s0 = bs[from + 0] & 0xFF ; |
| | | int s1 = bs[from + 1] & 0xFF ; |
| | | |
| | | // s1不变 |
| | | s0 <<= 8; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); |
| | | if (b) { |
| | | int s = 0; |
| | | int s0 = bs[from + 0] ; |
| | | int s1 = bs[from + 1] ; |
| | | int s0 = bs[from + 0] & 0xFF ; |
| | | int s1 = bs[from + 1] & 0xFF ; |
| | | |
| | | // s0不变 |
| | | s1 <<= 8; |
| | |
| | | * @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 |
| | |
| | | } |
| | | |
| | | 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 返回 转换后的字符串 |
| | | */ |
| | | 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]; |
| | | } |
| | | 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] ; |
| | | } |
| | | //一个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]; |
| | | } |
| | | return new String(buf); |
| | | } |
| | | |
| | | /** |
| | | * 将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; |
| | | } |
| | | // /** |
| | | // * 将byte[]转换为16进制字符串 |
| | | // * |
| | | // * @param bytes 待转换byte[] |
| | | // * @return 返回 转换后的字符串 |
| | | // */ |
| | | // 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]; |
| | | // } |
| | | // return new String(buf); |
| | | // } |
| | | // /** |
| | | // * 将byte[]转换为16进制字符串 |
| | | // * |
| | | // * @param bytes 待转换byte[] |
| | | // * @return 返回 转换后的字符串 |
| | | // */ |
| | | // public static String bytesToHex_BE(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] ; |
| | | // } |
| | | // //一个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]; |
| | | // } |
| | | // return new String(buf); |
| | | // } |
| | | // |
| | | // /** |
| | | // * 将byte[]转换为16进制字符串 |
| | | // * |
| | | // * @param bytes 待转换byte[] |
| | | // * @return 返回 转换后的字符串 |
| | | // */ |
| | | // public static String bytesToHex_LE(byte[] bytes, int startIndex, int endIndex) { |
| | | // byte[] bs = new byte[endIndex - startIndex + 1] ; |
| | | // byte j = 0 ; |
| | | // for(int i = endIndex; i >= startIndex; i--){ |
| | | // bs[j++] = bytes[i] ; |
| | | // } |
| | | // //一个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]; |
| | | // } |
| | | // return new String(buf); |
| | | // } |
| | | // |
| | | // /** |
| | | // * 将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 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 小端模式《数据低位在数组高字节》 |
| | | * 字符串转换成byte数组 |
| | | * @value bs byte[] |
| | | * @value str String |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |