左晓为主开发手持机充值管理机
zuoxiao
2024-05-09 b8f8323cc39091d3119101923251a0455da87f55
baselibrary/src/main/java/com/dayu/baselibrary/tools/HexUtil.java
@@ -3,6 +3,7 @@
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -29,9 +30,47 @@
        hexString = HighLowHex(hexString);
        return hexString;
    }
    /**
     * 16进制转10进制并且低位在前高位在后
     *
     * @param data
     * @return
     */
    public static int get16To10LowHightByBytes(byte[] data) {
        List<Byte> byteList = new ArrayList<>();
        for (byte b : data) {
            byteList.add(b);
        }
        Collections.reverse(byteList);
        byte[] byteArray = new byte[byteList.size()];
        for (int i = 0; i < byteList.size(); i++) {
            byteArray[i] = byteList.get(i);
        }
        String hex = bytesToHexNoAppen(byteArray);
        return Integer.parseInt(hex, 16);
    }
    /**
     * byte 数组转16进制字符串 不加0前缀
     *
     * @param bytes
     * @return
     */
    public static String bytesToHexNoAppen(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            sb.append(hex);
        }
        return sb.toString();
    }
    public static int getBcdToInt(byte data) {
        return ((data & 0xF0) >> 4) * 10 + ((data & 0x0F));
    }
    /**
     * short10进制转16进制 低位在前高位在后
     *
@@ -47,7 +86,6 @@
        while (hexString.length() < 4) {
            hexString = "0" + hexString;
        }
        hexString = spaceHex(hexString);
        hexString = HighLowHex(hexString);
        return hexString;
@@ -55,7 +93,7 @@
    /**
     * 将byte数组转换为带符号的32位浮点数
     *
     * <p>
     * 低位在前高位在后
     *
     * @param value
@@ -65,23 +103,9 @@
        ByteBuffer bufferLittleEndian = ByteBuffer.wrap(value);
        bufferLittleEndian.order(ByteOrder.LITTLE_ENDIAN);
        return bufferLittleEndian.getFloat();
    }
    /**
     * 将带符号的32位浮点数装换byte数组
     * 低位在前高位在后
     *
     * @param value
     * @return
     */
    public static byte[] folatToByte(Float value) {
        ByteBuffer buffer = ByteBuffer.allocate(4); // 4个字节
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.putFloat(value);
        byte[] byteArray = buffer.array();
        return byteArray;
    }
    /**
     * hex字符串转byte数组
     *
@@ -152,37 +176,6 @@
        return sb.toString();
    }
    public static String byteArrayToHexString(byte[] byteArray) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : byteArray) {
            // 将字节转换为无符号整数
            int unsignedInt = b & 0xff;
            // 将无符号整数转换为16进制字符串
            String hex = Integer.toHexString(unsignedInt);
            // 如果字符串长度小于2,在前面补0
            if (hex.length() < 2) {
                hex = "0" + hex;
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
    /**
     * 字节数组转16进制 不在末尾添加0
     *
     * @param bytes 需要转换的byte数组
     * @return 转换后的Hex字符串
     */
    public static String bytesToHexNoAddZero(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            sb.append(hex);
        }
        return sb.toString();
    }
    /**
     * 将 4字节的16进制字符串,转换为32位带符号的十进制浮点型
@@ -202,6 +195,37 @@
     */
    public static String folatToHexString(Float value) {
        return Integer.toHexString(Float.floatToIntBits(value));
    }
    /**
     * float转Hex低位在前高位在后
     *
     * @param value
     * @return
     */
    public static String floatToHexLowHigh(Float value) {
        String hexString = Integer.toHexString(Float.floatToIntBits(value));
        hexString = spaceHex(hexString);
        hexString = HighLowHex(hexString);
        return hexString;
    }
    public static float hexToFloatLowHigh(byte[] data) {
        List<Byte> byteList = new ArrayList<>();
        for (byte b : data) {
            byteList.add(b);
        }
        Collections.reverse(byteList);
        byte[] byteArray = new byte[byteList.size()];
        for (int i = 0; i < byteList.size(); i++) {
            byteArray[i] = byteList.get(i);
        }
        String hex = bytesToHexNoAppen(byteArray);
        int intValue = Integer.parseInt(hex, 16); // 将十六进制字符串转换为整数表示
        float floatValue = Float.intBitsToFloat(intValue); // 将整数表示转换为浮点数
        return floatValue;
    }
    /**
@@ -248,23 +272,6 @@
            e.printStackTrace();
        }
        return str;
    }
    /**
     * 16进制转10进制高低位转换
     * @param hex
     * @return
     */
    public static int get16to10LowHigh(String hex) {
        try {
            String str = "";
            str = spaceHex(hex);
            str = HighLowHex(str);
            return Integer.parseInt(str, 16);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }