2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-10-14 ea8e8e7dd5f10cff4054f5fde8fd3961aaac1834
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.sandu.ximon.admin.manager.iot.rrpc.util;
 
public class CRCUtils {
    /**
     * 计算CRC16/Modbus校验码  高位在前,低位在后
     *
     * @param hex 十六进制字符串
     * @return CRC16校验码
     */
    public static String getCRC16 (String hex) {
        byte[] tx_buff = SupplementUtils.hexStringToBytes(hex);
        int[] result = CRCValidate.calculateCRC(tx_buff, 0, tx_buff.length);
        String hex1 = Integer.toHexString(result[1]);
        String hex2 = Integer.toHexString(result[0]);
 
        if (hex1.length()<2) {
            hex1 = "0" + hex1;
        }
        if (hex2.length()<2) {
            hex2 = "0" + hex2;
        }
        return ( hex2 + hex1 ).toUpperCase();
    }
}