‘liusuyi’
2023-11-09 092d7c56bb8653075b8f4b27220e69e6bb8e5d37
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数组转Double
     */
    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方法将长整型转换为Double类型
        return Double.longBitsToDouble(longBits);
        // 创建一个ByteBuffer
        ByteBuffer buffer = ByteBuffer.wrap(byteArray);
        //设置字节顺序为大端
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        // 从ByteBuffer中获取double值
        double doubleValue = buffer.getDouble();
        return doubleValue;
    }
    /**
@@ -119,6 +138,31 @@
        return buffer.array();
    }
    /**
     * double转byte数组
     */
    public static byte[] doubleToBytes(double d) {
        // 创建一个 ByteBuffer,分配足够的空间来存储一个 float 值
        ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
        // 将 float 值写入 ByteBuffer
        buffer.putDouble(d);
        // 获取字节数组
        return buffer.array();
    }
    /**
     * float转byte数组
     */
    public static byte[] floatToBytes(float f) {
        // 创建一个 ByteBuffer,分配足够的空间来存储一个 float 值
        ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
        // 将 float 值写入 ByteBuffer
        buffer.putFloat(f);
        // 获取字节数组
        return buffer.array();
    }
    /**
     * byte数组拼接
     */