From 4fe7ea4fbdc0c45f14d1d8de77e3424f826ba909 Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期五, 28 六月 2024 21:39:12 +0800 Subject: [PATCH] 1、靳总协议上行数据处理修改完善; 2、上行数据处理任务修改完善. --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java | 281 +++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 185 insertions(+), 96 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java index d981669..f3f8dfc 100644 --- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java @@ -892,47 +892,107 @@ * @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 @@ -976,77 +1036,106 @@ } private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - /** - * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓� - * - * @param bytes 寰呰浆鎹yte[] - * @return 杩斿洖 杞崲鍚庣殑瀛楃涓� - */ - public static String bytesToHex(byte[] bytes) { - //涓�涓猙yte涓�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); - } - /** - * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓� - * - * @param bytes 寰呰浆鎹yte[] - * @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] ; - } - //涓�涓猙yte涓�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; - } +// /** +// * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓� +// * +// * @param bytes 寰呰浆鎹yte[] +// * @return 杩斿洖 杞崲鍚庣殑瀛楃涓� +// */ +// public static String bytesToHex(byte[] bytes) { +// //涓�涓猙yte涓�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); +// } +// /** +// * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓� +// * +// * @param bytes 寰呰浆鎹yte[] +// * @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] ; +// } +// //涓�涓猙yte涓�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); +// } +// +// /** +// * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓� +// * +// * @param bytes 寰呰浆鎹yte[] +// * @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] ; +// } +// //涓�涓猙yte涓�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 -- Gitblit v1.8.0