From 092d7c56bb8653075b8f4b27220e69e6bb8e5d37 Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: 星期四, 09 十一月 2023 16:49:26 +0800
Subject: [PATCH] 增加超脑业务

---
 src/main/java/com/ard/utils/other/ByteUtils.java |   66 +++++++++++++++++++++++++++-----
 1 files changed, 55 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..0c11f68 100644
--- a/src/main/java/com/ard/utils/other/ByteUtils.java
+++ b/src/main/java/com/ard/utils/other/ByteUtils.java
@@ -1,9 +1,14 @@
 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.nio.charset.StandardCharsets;
 import java.util.zip.CRC32;
+
+import static oracle.security.pki.util.SignatureAlgorithms.i;
 
 /**
  * @Description: 瀛楄妭宸ュ叿绫�
@@ -13,10 +18,25 @@
  * @Version: 1.0
  **/
 public class ByteUtils {
+
+    /**
+     * 鎵撳嵃鍗佸叚杩涘埗浜岃繘鍒�
+     */
+    public static String printHexBinary(byte[] bytes) {
+        return DatatypeConverter.printHexBinary(bytes);
+    }
+
     /**
      * byte鏁扮粍杞腑鏂囧瓧绗︿覆
      */
     public static String bytesToStringZh(byte[] bytes) {
+        //ByteBuffer buffer = ByteBuffer.wrap(bytes); // byteArray 鏄寘鍚瓧鑺傛暟缁勭殑鍙橀噺
+        //try {
+        //    return new String(buffer.array(), "GBK");
+        //} catch (UnsupportedEncodingException e) {
+        //    e.printStackTrace();
+        //}
+        //return "";
         String zhStr = "";
         try {
             int position = ByteUtils.findIndexOfDoubleZero(bytes);
@@ -61,28 +81,27 @@
      * byte鏁扮粍杞暣鍨�
      */
     public static int bytesToDecimal(byte[] byteArray) {
+        //ByteBuffer buffer = ByteBuffer.wrap(byteArray); // byteArray 鏄寘鍚瓧鑺傛暟缁勭殑鍙橀噺
+        //return buffer.getInt();
         int decimalValue = 0;
-
         for (int i = 0; i < byteArray.length; i++) {
             decimalValue = (decimalValue << 8) | (byteArray[i] & 0xFF);
         }
-
         return decimalValue;
     }
+
 
     /**
      * 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;
     }
 
     /**
@@ -119,6 +138,31 @@
         return buffer.array();
     }
 
+
+    /**
+     * double杞琤yte鏁扮粍
+     */
+    public static byte[] doubleToBytes(double d) {
+        // 鍒涘缓涓�涓� ByteBuffer锛屽垎閰嶈冻澶熺殑绌洪棿鏉ュ瓨鍌ㄤ竴涓� float 鍊�
+        ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
+        // 灏� float 鍊煎啓鍏� ByteBuffer
+        buffer.putDouble(d);
+        // 鑾峰彇瀛楄妭鏁扮粍
+        return buffer.array();
+    }
+
+    /**
+     * 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鏁扮粍鎷兼帴
      */

--
Gitblit v1.9.3