From 7fd3ff6715939467e3850763206d37fac8564e41 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期一, 02 十二月 2024 11:15:26 +0800
Subject: [PATCH] 修改升级协议中的bug,未在下行数据中放入帧长度
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java | 181 ++++++++++++++++++++++++++++++++++++++------
1 files changed, 155 insertions(+), 26 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java
index 8635450..4a6e509 100644
--- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java
@@ -1,6 +1,5 @@
package com.dy.common.util;
-@SuppressWarnings("unuseed")
public class ByteUtilUnsigned {
/**
* 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
@@ -36,7 +35,7 @@
throw new Exception("int2Bytes鏃舵暟缁勮秺鐣�");
}
}
-
+
/**
* 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
@@ -61,7 +60,7 @@
}
boolean b = isOutOfArrLength(bs.length, (from - 1) + len);
if (!b) {
- for (int i = 0; i > len ; i++) {
+ for (int i = 0; i <= len ; i++) {
bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();// 灏嗘渶浣庝綅淇濆瓨鍦ㄤ綆瀛楄妭
temp = temp >> 8; // 鍚戝彸绉�8浣�
}
@@ -131,6 +130,126 @@
throw new Exception("byte2Int鏃舵暟缁勮秺鐣�");
}
}
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 涓庢柟娉昩ytes2Int绠楁硶涓�鏍凤紝鍙槸鎶婇『搴忓弽杩囨潵
+ * @param bs 瀛楄妭鏁扮粍
+ * @param from 瀛楄妭鏁扮粍璧峰浣嶇疆
+ * @param has3Byte 鏄惁鍖呭惈鏁版嵁涓嬫爣涓�3鐨勫瓧鑺傦紝濡傛灉涓嶅寘鍚疄闄呭鐞嗙殑鏄�3瀛楄妭鐨勬暣鏁�
+ * @return
+ */
+ public static long bytes2Int_LE(byte[] bs, int from, boolean has3Byte) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
+ if (!b) {
+ long s = 0;
+ long s0 = bs[from + 0] & 0xFF ;// 鏁版嵁鐨勬渶浣庝綅鍦ㄤ綆瀛楄妭
+ long s1 = bs[from + 1] & 0xFF ;
+ long s2 = bs[from + 2] & 0xFF ;
+ long s3 = 0L ;
+ if(!has3Byte){
+ s3 = bs[from + 3] & 0xFF ;
+ }
+
+
+ // S0涓嶅彉
+ s1 <<= 8;
+ s2 <<= 16;
+ s3 <<= 24;
+ s = s0 | s1 | s2 | s3;
+ if(s < 0){
+ //s = Integer.MAX_VALUE -s ;
+ s = Integer.MAX_VALUE * 2 + 1 + s + 1 ;
+ }
+ return s;
+ } else {
+ throw new Exception("byte2Int鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 涓庢柟娉昩ytes2Int绠楁硶涓�鏍凤紝鍙槸鎶婇『搴忓弽杩囨潵
+ * @param bs
+ * @return
+ */
+ public static long bytes2Long_LE(byte[] bs, int startIndex, int endIndex) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, endIndex);
+ if (!b) {
+ byte count = 0 ;
+ long s = 0;
+ long temp = 0 ;
+ for(int i = startIndex ; i <= endIndex; i++){
+ temp = bs[i] & 0xFF ;// 鏁版嵁鐨勬渶浣庝綅鍦ㄤ綆瀛楄妭
+ if(temp < 0){
+ temp = Byte.MAX_VALUE * 2 + 1 + temp + 1 ;
+ }
+ if(count > 0){
+ temp <<= count * 8;
+ }
+ count++ ;
+ s = s | temp ;
+ }
+ return s;
+ } else {
+ throw new Exception("bytes2Long_LE鏃舵暟缁勮秺鐣�");
+ }
+ }
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 鏃犵鍙穝hort绫诲瀷杞崲鎴�2浣峛yte鏁扮粍
+ * java娌℃湁鏃犵鍙风煭鏁村瀷鏁版嵁锛屽彧鏈夋湁绗﹀彿鐭暣鏁帮紝鍙栧�艰寖鍥存槸-32768~32767
+ * 鑻ユā鎷熸棤绗﹀彿鐭暣鍨嬫暟鎹紝鍙栧�艰寖鍥存槸0鍒�65535 锛屽凡缁忚秴鍑轰簡java鐨勬湁绗﹀彿鏁存暟涓婇檺锛屾墍浠ュ彧鑳界敤java鐨処nt鍨嬭〃绀烘棤绗﹀彿鏁存暟
+ * @value bs byte[]
+ */
+ public static long bytes2Long_BE(byte[] bs, int startIndex, int endIndex) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, endIndex);
+ if (!b) {
+ int count = endIndex - startIndex ;
+ long s = 0;
+ long temp = 0 ;
+ for(int i = startIndex ; i <= endIndex ; i++){
+ temp = bs[i] & 0xFF ;// 鏁版嵁鐨勬渶浣庝綅鍦ㄤ綆瀛楄妭
+ if(temp < 0){
+ temp = Byte.MAX_VALUE * 2 + 1 + temp + 1 ;
+ }
+ if(count > 0){
+ temp <<= count * 8;
+ }
+ count-- ;
+ s = s | temp ;
+ }
+ return s;
+ } else {
+ throw new Exception("bytes2Long_LE鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+// public static long bytes2Int_BE(byte[] bs, int from) throws Exception {
+// boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
+// if (!b) {
+// long s = 0;
+// long s0 = bs[from + 0] & 0xFF ;// 鏁版嵁鐨勬渶楂樹綅鍦ㄤ綆瀛楄妭
+// long s1 = bs[from + 1] & 0xFF ;
+// long s2 = bs[from + 2] & 0xFF ;
+// long s3 = bs[from + 3] & 0xFF ;
+//
+// // 鏈�浣庝綅S3涓嶅彉
+// s0 <<= 24;
+// s1 <<= 16;
+// s2 <<= 8;
+// s = s0 | s1 | s2 | s3;
+// if(s < 0){
+// //s = Integer.MAX_VALUE -s ;
+// s = Integer.MAX_VALUE * 2 + 1 + s + 1 ;
+// }
+// return s;
+// } else {
+// throw new Exception("byte2Int鏃舵暟缁勮秺鐣�");
+// }
+// }
+
/**
* 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
* 鏃犵鍙穝hort绫诲瀷杞崲鎴�2浣峛yte鏁扮粍
@@ -244,7 +363,7 @@
throw new Exception("bytes2Short鏃舵暟缁勮秺鐣�");
}
}
-
+
/**
* 1浣嶅瓧鑺傛暟缁勮浆鎹负鐭暣鍨�
@@ -256,7 +375,7 @@
if (bs.length - 1 < index) {
throw new Exception("byte2Short(byte[] bs, int index)鏃舵暟缁勮秺鐣�");
} else {
- byte bv = (byte)(bs[index] & 0xff) ;
+ byte bv = (byte)(bs[index] & 0xff) ;
short s = 0 ;
if(bv < 0){
s = (short)(Byte.MAX_VALUE * 2 + 1 + bv + 1) ;
@@ -286,7 +405,7 @@
bs[index] = Integer.valueOf(value & 0xff).byteValue() ;
}
}
-
+
/**
* 鍒ゆ柇鎵�鏈夊瓧鑺傛槸鍚︿负0xFF
* @param bs 瀛楄妭鏁扮粍
@@ -307,7 +426,7 @@
/**
* 鍒ゆ柇鏁扮粍涓嬫爣鏄惁瓒婄晫
- *
+ *
* @value bsLength 鏁扮粍鎬婚暱搴�
* @value toSite 鏁扮粍鍋忕Щ閲�
* @return 缁撴灉
@@ -319,27 +438,37 @@
return true;
}
}
+// public static void main(String[] args) throws Exception{
+// byte[] bs = new byte[]{(byte)0x38,(byte)0x1d,(byte)0x00,(byte)0x00,(byte)0x00} ;
+// Long lg1 = ByteUtilUnsigned.bytes2Long_LE(bs, 0, 4) ;
+// Long lg2 = ByteUtilUnsigned.bytes2Long_BE(bs, 0, 4) ;
+// System.out.println(lg1);
+// System.out.println(lg2);
+// }
- /*
public static void main(String[] args) throws Exception{
-// int d = 123456;
-// byte[] bs = new byte[4] ;
-// int2Bytes_BE(bs, d, 0) ;
-// System.out.println(ByteUtil.bytes2Hex(bs, false));
-// long dd = bytes2Int_BE(bs, 0) ;
-// System.out.println(dd);
-//
-// byte[] bb = new byte[1] ;
-// bb[0] = (byte)255 ;
-// short s = byte2Byte(bb, 0);
-// System.out.println(s);
-
- byte[] bs = new byte[]{(byte)0x00, (byte)0x00, (byte)0x3A, (byte)0x88} ;
- //byte[] bs = new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff} ;
- boolean flag = ByteUtilUnsigned.bytesIsAll0xFF(bs, 0, 4) ;
- System.out.println(flag);
- Long s = ByteUtilUnsigned.bytes2Int_BE(bs, 0);
+ byte[] bs = new byte[]{0x02, 0x0C} ;
+ int s = ByteUtilUnsigned.bytes2Short_BE(bs, 0) ;
System.out.println(s);
+//// int d = 123456;
+//// byte[] bs = new byte[4] ;
+//// int2Bytes_BE(bs, d, 0) ;
+//// System.out.println(ByteUtil.bytes2Hex(bs, false));
+//// long dd = bytes2Int_BE(bs, 0) ;
+//// System.out.println(dd);
+////
+//// byte[] bb = new byte[1] ;
+//// bb[0] = (byte)255 ;
+//// short s = byte2Byte(bb, 0);
+//// System.out.println(s);
+//
+// byte[] bs = new byte[]{(byte)0x00, (byte)0x00, (byte)0x3A, (byte)0x88} ;
+// //byte[] bs = new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff} ;
+// boolean flag = ByteUtilUnsigned.bytesIsAll0xFF(bs, 0, 4) ;
+// System.out.println(flag);
+// Long s = ByteUtilUnsigned.bytes2Int_BE(bs, 0);
+// System.out.println(s);
+//
+//
}
- */
}
--
Gitblit v1.8.0