From 616e0764ed9e61ab8a2e8b523eecbba6b98c5c13 Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: 星期三, 01 十一月 2023 17:09:55 +0800
Subject: [PATCH] 增加告警前端发送的强制引导信息接收并发送mqtt

---
 src/main/java/com/ard/utils/other/ByteUtils.java |   49 ++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/ard/utils/other/ByteUtils.java b/src/main/java/com/ard/utils/other/ByteUtils.java
index 752b333..f89e0e9 100644
--- a/src/main/java/com/ard/utils/other/ByteUtils.java
+++ b/src/main/java/com/ard/utils/other/ByteUtils.java
@@ -1,8 +1,10 @@
 package com.ard.utils.other;
 
+import javax.xml.bind.DatatypeConverter;
 import java.io.ByteArrayOutputStream;
 import java.io.UnsupportedEncodingException;
 import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
 import java.util.zip.CRC32;
 
 /**
@@ -13,6 +15,14 @@
  * @Version: 1.0
  **/
 public class ByteUtils {
+
+    /**
+     * 鎵撳嵃鍗佸叚杩涘埗浜岃繘鍒�
+     */
+    public static String printHexBinary(byte[] bytes) {
+        return DatatypeConverter.printHexBinary(bytes);
+    }
+
     /**
      * byte鏁扮粍杞腑鏂囧瓧绗︿覆
      */
@@ -62,11 +72,9 @@
      */
     public static int bytesToDecimal(byte[] byteArray) {
         int decimalValue = 0;
-
         for (int i = 0; i < byteArray.length; i++) {
             decimalValue = (decimalValue << 8) | (byteArray[i] & 0xFF);
         }
-
         return decimalValue;
     }
 
@@ -74,15 +82,12 @@
      * byte鏁扮粍杞珼ouble
      */
     public static double bytesToDouble(byte[] byteArray) {
-        long longBits = 0;
-
-        // 鏍规嵁瀛楄妭鏁扮粍鐨勯暱搴﹀拰瀛楄妭椤哄簭锛屽皢瀛楄妭鏁扮粍杞崲涓洪暱鏁村瀷
-        for (int i = 0; i < byteArray.length; i++) {
-            longBits |= (long) (byteArray[i] & 0xFF) << (8 * (byteArray.length - 1 - i));
-        }
-
-        // 浣跨敤Double.longBitsToDouble鏂规硶灏嗛暱鏁村瀷杞崲涓篋ouble绫诲瀷
-        return Double.longBitsToDouble(longBits);
+        // 鍒涘缓涓�涓狟yteBuffer骞惰缃瓧鑺傞『搴忎负澶х
+        ByteBuffer buffer = ByteBuffer.wrap(byteArray);
+        buffer.order(ByteOrder.LITTLE_ENDIAN);
+        // 浠嶣yteBuffer涓幏鍙杁ouble鍊�
+        double doubleValue = buffer.getDouble();
+        return doubleValue;
     }
 
     /**
@@ -120,6 +125,28 @@
     }
 
     /**
+     * double杞琤yte鏁扮粍
+     */
+    public static byte[] doubleToBytes(double d) {
+        long value = Double.doubleToRawLongBits(d);
+        byte[] byteRet = new byte[8];
+        for (int i = 0; i < 8; i++) {
+            byteRet[i] = (byte) ((value >> 8 * i) & 0xff);
+        }
+        return byteRet;
+    }
+    /**
+     * float杞琤yte鏁扮粍
+     */
+    public static byte[] floatToBytes(float f) {
+        // 鍒涘缓涓�涓� ByteBuffer锛屽垎閰嶈冻澶熺殑绌洪棿鏉ュ瓨鍌ㄤ竴涓� float 鍊�
+        ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
+        // 灏� float 鍊煎啓鍏� ByteBuffer
+        buffer.putFloat(f);
+        // 鑾峰彇瀛楄妭鏁扮粍
+        return buffer.array();
+    }
+    /**
      * byte鏁扮粍鎷兼帴
      */
     public static byte[] appendArrays(byte[]... arrays) {

--
Gitblit v1.9.3