From 3cf13ad3251d86fa4025e4a248f600c6b56c7e8f Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期六, 12 十月 2024 17:11:45 +0800 Subject: [PATCH] 江海协议相关完善代码 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java | 155 +++++++++++++++++---------------------------------- 1 files changed, 51 insertions(+), 104 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 f3f8dfc..2f0c7d6 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 @@ -511,9 +511,9 @@ 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鏃舵暟缁勮秺鐣�"); @@ -1035,107 +1035,29 @@ return fromIndex ; } - 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_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; -// } + /** + * 鍗佸叚杩涘埗杞瓧鑺傛暟缁� + * @param hex the hex string + * @return 杩斿洖 byte[] + */ + public static int hex2Bytes_LE(String hex, byte[] bs, int fromIndex) { + if (hex == null || hex.equals("")) { + return fromIndex; + } + 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])); + } + for(int i = 0 ; i < d.length; i++){ + bs[fromIndex++] = d[(d.length - 1) - i] ; + } + return fromIndex ; + } + /** * Convert char to byte @@ -1185,6 +1107,31 @@ } /** + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘�� + * 鏁村舰杞垚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] ; + } + } + + /** * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘�� * 闀挎暣褰㈣浆鎴怋CD缂栫爜 * @param l -- Gitblit v1.8.0