• 物联网平台中常用的一些Java工具类


    后续会不断补充…

    一、字节数组 to 字符串;字符串 to 字节数组

    字节数组 to 字符串

        /**
         * 将字节数组转换为16进制字符串
         * byte[] to string
         * @param bytes 字节数组
         * @return 字符串,返回值:01 03 02 01 0F F9 D0
         */
        public static String byteArrayToHexString(byte[] bytes) {
            if (isEmpty(bytes)) {
                return null;
            }
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String hex = String.valueOf(HEX_STR.charAt((b & 0xF0) >> 4));
                hex += String.valueOf(HEX_STR.charAt(b & 0x0F));
                result.append(hex + " ");
            }
            System.out.println(MessageFormat.format("字节数组 To 16进制字符串完成!结果为:[{0}]", result));
            return result.toString();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    字符串 to 字节数组

    	/**
         * string to byte[]
         * 字符串转换为字节数组
         * @param hexString 16进制表示的字符串
         * @return byte[] 字节数组
         */
        public static byte[] hexStringToByteArray(String hexString) {
            if (StringUtils.isEmpty(hexString)) {
                return null;
            }
            System.out.println(MessageFormat.format("16进制字符串:[{0}] To 字节数组开始...",hexString));
            hexString = hexString.replaceAll(" ", "");
            int len = hexString.length();
            byte[] bytes = new byte[len / 2];
            for (int i = 0; i < len; i += 2) {
                // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
                bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                        .digit(hexString.charAt(i + 1), 16));
            }
            return bytes;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    二、Modbus CRC16的校验算法工具类

    很多传感器指令都是如下格式:01 06 00 11 00 1E 59 C7
    前面的是地址位+操作码+数据长度+数据体,一般最后的59 C7属于校验位,是通过前面的数据算出来的,所以我们可以根据前面的数据算出后面的校验位。

    代码如下:

    /**
     * /**
     *
     * @desc 基于Modbus CRC16的校验算法工具类
     * 例如01 06 00 64 00 02会自动补全CRC16高位、CRC16低位
     * 变成01 06 00 64 00 02 49 D4
     * @create 2020/09/04
     */
    public class Crc16Utils {
    
        public static byte[] getByteArray(String command) {
            byte[] byteArray = new byte[]{};
            for (String hex : command.split(" ")) {
                //当前16进制转为10进制数
                byte[] currentByte = {(byte) Integer.parseInt(hex, 16)};
                byte[] buffer = new byte[byteArray.length + 1];
                System.arraycopy(byteArray, 0, buffer, 0, byteArray.length);
                System.arraycopy(currentByte, 0, buffer, byteArray.length, currentByte.length);
                byteArray = buffer;
            }
            return getByteArray(byteArray);
        }
    
        /**
         * 获取源数据和验证码的组合byte数组
         *
         * @param byteArray 字节数组
         * @return
         */
        private static byte[] getByteArray(byte[] byteArray) {
            byte[] transferByteArray = getCrc16ByteArray(byteArray);
            byte[] lastedByteArray = new byte[byteArray.length + transferByteArray.length];
            System.arraycopy(byteArray, 0, lastedByteArray, 0, byteArray.length);
            System.arraycopy(transferByteArray, 0, lastedByteArray, byteArray.length, transferByteArray.length);
            return lastedByteArray;
        }
    
        /**
         * 获取验证码byte数组,基于Modbus CRC16的校验算法
         *
         * @param bufferByteArray
         * @return
         */
        private static byte[] getCrc16ByteArray(byte[] bufferByteArray) {
            // 预置 1 个 16 位的寄存器为十六进制FFFF, 称此寄存器为 CRC寄存器。
            int crc = 0xFFFF;
            for (int i = 0, len = bufferByteArray.length; i < len; i++) {
                // 把第一个 8 位二进制数据 与 16 位的 CRC寄存器的低 8 位相异或, 把结果放于 CRC寄存器
                crc = ((crc & 0xFF00) | (crc & 0x00FF) ^ (bufferByteArray[i] & 0xFF));
                for (int j = 0; j < 8; j++) {
                    // 把 CRC 寄存器的内容右移一位( 朝低位)用 0 填补最高位, 并检查右移后的移出位
                    if ((crc & 0x0001) > 0) {
                        // 如果移出位为 1, CRC寄存器与多项式A001进行异或
                        crc = crc >> 1;
                        crc = crc ^ 0xA001;
                    } else {
                        // 如果移出位为 0,再次右移一位
                        crc = crc >> 1;
                    }
                }
            }
            return intToBytes(crc);
        }
    
        /**
         * 将int转换成byte数组,低位在前,高位在后
         * 改变高低位顺序只需调换数组序号
         *
         * @param value
         */
        private static byte[] intToBytes(int value) {
            byte[] src = new byte[2];
            src[1] = (byte) ((value >> 8) & 0xFF);
            src[0] = (byte) (value & 0xFF);
            return src;
        }
    
        /**
         * 将字节数组转换成十六进制字符串
         *
         * @param byteArray
         */
        public static String byteTo16String(byte[] byteArray) {
            StringBuffer buffer = new StringBuffer();
            for (byte b : byteArray) {
                buffer.append(byteTo16String(b));
            }
            return buffer.toString();
        }
    
        /**
         * 将字节转换成十六进制字符串
         * int转byte对照表
         * [128,255],0,[1,128)
         * [-128,-1],0,[1,128)
         *
         * @param tempByte
         */
        public static String byteTo16String(byte tempByte) {
            StringBuffer buffer = new StringBuffer();
            int tempInt = (int) tempByte;
            if (tempInt < 0) {
                buffer.append(Integer.toString(tempInt + 256, 16) + " ");
            } else if (tempInt == 0) {
                buffer.append("00 ");
            } else if (tempInt > 0 && tempInt <= 15) {
                buffer.append("0" + Integer.toString(tempInt, 16) + " ");
            } else if (tempInt > 15) {
                buffer.append(Integer.toString(tempInt, 16) + " ");
            }
            return buffer.toString();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
  • 相关阅读:
    【leetcode】三数之和
    MySQL事务详解
    URP渲染管线实战教程系列 之URP渲染管线实战解密(一)
    『亚马逊云科技产品测评』活动征文|AWS 存储产品类别及其适用场景详细说明
    什么是RFP?什么又是CFP?两者的含金量该如何看?
    数据结构与算法之 leetcode 47. 全排列 II (回溯)
    C++手敲灰度图均值滤波中值滤波高斯滤波
    day15学习总结
    驱动开发,基于中断子系统完成按键的中断驱动,引入中断底半部
    手写本地缓存实战2—— 打造正规军,构建通用本地缓存框架
  • 原文地址:https://blog.csdn.net/weixin_33005117/article/details/126153573