From 81d297dd33911dbfdc93dbc3fa9747e511b7ce8e Mon Sep 17 00:00:00 2001
From: panlili2024 <14743743+panlili2024@user.noreply.gitee.com>
Date: 星期五, 08 十一月 2024 14:51:10 +0800
Subject: [PATCH] 优化透传数据解析

---
 HDL_TTLSDK485/src/main/java/com/hdl/sdk/ttl/Utils/HDLUtlis/HDLUtlis.java |   88 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 71 insertions(+), 17 deletions(-)

diff --git a/HDL_TTLSDK485/src/main/java/com/hdl/sdk/ttl/Utils/HDLUtlis/HDLUtlis.java b/HDL_TTLSDK485/src/main/java/com/hdl/sdk/ttl/Utils/HDLUtlis/HDLUtlis.java
index 8bd411c..5cf4442 100644
--- a/HDL_TTLSDK485/src/main/java/com/hdl/sdk/ttl/Utils/HDLUtlis/HDLUtlis.java
+++ b/HDL_TTLSDK485/src/main/java/com/hdl/sdk/ttl/Utils/HDLUtlis/HDLUtlis.java
@@ -2,6 +2,7 @@
 
 import java.math.BigDecimal;
 import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
 
 /**
  * Created by JLChen on 2019/7/25
@@ -10,24 +11,25 @@
 
     /**
      * 灏唎bject杞负Integer绫诲瀷
+     *
      * @param object
      * @return
      */
-    public static Integer getIntegerByObject(Object object){
+    public static Integer getIntegerByObject(Object object) {
         Integer in = null;
-        if(object!=null){
-            if(object instanceof Integer){
-                in = (Integer)object;
-            }else if(object instanceof String){
-                in = Integer.parseInt((String)object);
-            }else if(object instanceof Double){
-                in = (int)((double)object);
-            }else if(object instanceof Float){
-                in = (int)((float)object);
-            }else if(object instanceof BigDecimal){
-                in = ((BigDecimal)object).intValue();
-            }else if(object instanceof Long){
-                in = ((Long)object).intValue();
+        if (object != null) {
+            if (object instanceof Integer) {
+                in = (Integer) object;
+            } else if (object instanceof String) {
+                in = Integer.parseInt((String) object);
+            } else if (object instanceof Double) {
+                in = (int) ((double) object);
+            } else if (object instanceof Float) {
+                in = (int) ((float) object);
+            } else if (object instanceof BigDecimal) {
+                in = ((BigDecimal) object).intValue();
+            } else if (object instanceof Long) {
+                in = ((Long) object).intValue();
             }
         }
         return in;
@@ -35,6 +37,7 @@
 
     /**
      * int绫诲瀷杞�4瀛楄妭byte鏁扮粍
+     *
      * @param mInt
      * @return 4瀛楄妭byte鏁扮粍
      */
@@ -50,6 +53,7 @@
 
     /**
      * byte[]杞琲nt
+     *
      * @param bytes
      * @return
      */
@@ -65,12 +69,12 @@
 
 
     public static float byte2Float(byte[] bytes) {
-        if (bytes.length != 4) {
+       /* if (bytes.length != 4) {
             return 0;
-        }
+        }*/
         byte b[] = bytes;
         ByteBuffer buf = ByteBuffer.allocateDirect(4);
-//		buf=buf.order(ByteOrder.LITTLE_ENDIAN);灏忕鐢ㄨ繖琛屼唬鐮侊紝榛樿澶х杞崲
+        buf = buf.order(ByteOrder.LITTLE_ENDIAN);//灏忕鐢ㄨ繖琛屼唬鐮侊紝榛樿澶х杞崲
         buf.put(b);
         buf.rewind();
         float f2 = buf.getFloat();
@@ -79,6 +83,7 @@
 
     /**
      * 璋冩暣int 绫诲瀷鍙傛暟
+     *
      * @return progress
      */
     public static int getTrueProgressInt(int progress) {
@@ -89,4 +94,53 @@
         }
         return progress;
     }
+
+
+    /**
+     * 4 byte 杞崲涓篺loat绫诲瀷
+     *
+     * @param b1
+     * @param b2
+     * @param b3
+     * @param b4
+     * @return
+     */
+    public static float byteToFloat(byte b1, byte b2, byte b3, byte b4) {
+        byte[] mByte = new byte[4];
+        mByte[0] = b1;
+        mByte[1] = b2;
+        mByte[2] = b3;
+        mByte[3] = b4;
+        return byteArrayToFloat(mByte);
+    }
+
+
+    /**
+     * byte[4]鏁扮粍 杞崲涓篺loat绫诲瀷
+     *
+     * @param arr 闀垮害涓�4
+     * @return
+     */
+    public static float byteArrayToFloat(byte[] arr) {
+        try {
+            return Float.intBitsToFloat(getInt(arr));
+        } catch (Exception e) {
+            e.printStackTrace();
+            return 0;
+        }
+    }
+
+
+    /**
+     * 杩炵画4涓瓧鑺傝幏寰椾竴涓猧nt
+     *
+     * @param arr
+     * @return int
+     */
+    public static int getInt(byte[] arr) {
+        return (0xff000000 & (arr[0] << 24)) |
+                (0x00ff0000 & (arr[1] << 16)) |
+                (0x0000ff00 & (arr[2] << 8)) |
+                (0x000000ff & arr[3]);
+    }
 }

--
Gitblit v1.8.0