From 6474b923d8f6d8ea5e5b63277f18335136c7e33c Mon Sep 17 00:00:00 2001 From: ‘liusuyi’ <1951119284@qq.com> Date: 星期一, 04 十二月 2023 11:24:16 +0800 Subject: [PATCH] 增加动态客户端备用代码 优化雷达追踪引导 优化打印日志 --- src/main/java/com/ard/utils/other/ByteUtils.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ard/utils/other/ByteUtils.java b/src/main/java/com/ard/utils/other/ByteUtils.java index b82cd85..fe2ab19 100644 --- a/src/main/java/com/ard/utils/other/ByteUtils.java +++ b/src/main/java/com/ard/utils/other/ByteUtils.java @@ -1,10 +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: 瀛楄妭宸ュ叿绫� @@ -14,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); @@ -62,6 +81,8 @@ * 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); @@ -69,12 +90,14 @@ return decimalValue; } + /** * byte鏁扮粍杞珼ouble */ public static double bytesToDouble(byte[] byteArray) { - // 鍒涘缓涓�涓狟yteBuffer骞惰缃瓧鑺傞『搴忎负澶х + // 鍒涘缓涓�涓狟yteBuffer ByteBuffer buffer = ByteBuffer.wrap(byteArray); + //璁剧疆瀛楄妭椤哄簭涓哄ぇ绔� buffer.order(ByteOrder.LITTLE_ENDIAN); // 浠嶣yteBuffer涓幏鍙杁ouble鍊� double doubleValue = buffer.getDouble(); @@ -115,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鏁扮粍鎷兼帴 */ @@ -141,7 +189,12 @@ // System.out.print("鏍¢獙:"+DatatypeConverter.printHexBinary(bytes));//鎵撳嵃crc32鐨勬牎楠屽�� return bytes; } - + /** + * byte杞簩杩涘埗瀛楃涓� + */ + public static String byteToBitString(byte b) { + return String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); + } /** * 鍘婚櫎鍖呭ご鍖呭熬 * 鍒樿嫃涔� -- Gitblit v1.9.3