Blame view

zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/CRC16Util.java 2.51 KB
b5a79ddd   zhaowg   电信IOT插件
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
  package com.zteits.nbiot.decoder;
  
  public class CRC16Util {
  
      /**
       * 获取源数据和验证码的组合byte数组
       *
       * @param strings 可变长度的十六进制字符串
       * @return
       */
      public static byte[] appendCrc16(String... strings) {
          byte[] data = new byte[]{};
          for (int i = 0; i < strings.length; i++) {
              int x = Integer.parseInt(strings[i], 16);
              byte n = (byte) x;
              byte[] buffer = new byte[data.length + 1];
              byte[] aa = {n};
              System.arraycopy(data, 0, buffer, 0, data.length);
              System.arraycopy(aa, 0, buffer, data.length, aa.length);
              data = buffer;
          }
          return appendCrc16(data);
      }
  
      /**
       * 获取源数据和验证码的组合byte数组
       *
       * @param aa 字节数组
       * @return
       */
      public static byte[] appendCrc16(byte[] aa) {
          byte[] bb = getCrc16(aa);
          byte[] cc = new byte[aa.length + bb.length];
          System.arraycopy(aa, 0, cc, 0, aa.length);
          System.arraycopy(bb, 0, cc, aa.length, bb.length);
          return cc;
      }
  
      /**
       * 获取验证码byte数组,基于Modbus CRC16的校验算法
       */
      public static byte[] getCrc16(byte[] arr_buff) {
          int len = arr_buff.length;
  
          // 预置 1 个 16 位的寄存器为十六进制FFFF, 称此寄存器为 CRC寄存器。
          int crc = 0xFFFF;
          int i, j;
          for (i = 0; i < len; i++) {
              // 把第一个 8 位二进制数据 与 16 位的 CRC寄存器的低 8 位相异或, 把结果放于 CRC寄存器
              crc = ((crc & 0xFF00) | (crc & 0x00FF) ^ (arr_buff[i] & 0xFF));
              for (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数组,低位在前,高位在后
       * 改变高低位顺序只需调换数组序号
       */
      private static byte[] intToBytes(int value) {
          byte[] src = new byte[2];
          src[1] = (byte) ((value >> 8) & 0xFF);
          src[0] = (byte) (value & 0xFF);
          return src;
      }
  }