‘liusuyi’
2023-11-09 092d7c56bb8653075b8f4b27220e69e6bb8e5d37
src/main/java/com/ard/utils/other/ByteUtils.java
@@ -30,13 +30,27 @@
     * byte数组转中文字符串
     */
    public static String bytesToStringZh(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes); // byteArray 是包含字节数组的变量
        //ByteBuffer buffer = ByteBuffer.wrap(bytes); // byteArray 是包含字节数组的变量
        //try {
        //    return new String(buffer.array(), "GBK");
        //} catch (UnsupportedEncodingException e) {
        //    e.printStackTrace();
        //}
        //return "";
        String zhStr = "";
        try {
            return new String(buffer.array(), "GBK");
            int position = ByteUtils.findIndexOfDoubleZero(bytes);
            if (position != -1) {
                byte[] result = new byte[position];
                System.arraycopy(bytes, 0, result, 0, position);
                zhStr = new String(result, "GBK");
            } else {
                zhStr = new String(bytes, "GBK");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
        return zhStr;
    }
    /**
@@ -67,8 +81,13 @@
     * byte数组转整型
     */
    public static int bytesToDecimal(byte[] byteArray) {
        ByteBuffer buffer = ByteBuffer.wrap(byteArray); // byteArray 是包含字节数组的变量
        return buffer.getInt();
        //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;
    }