帖子
帖子
用户
博客
课程
显示全部楼层
2
帖子
0
勋章
16
Y币
发表于 2018-4-24 23:01:42
2
已解决

如何导入JAVA源码的CRC8校验 [复制链接]

  • 发表于:2018-04-24 23:01:42
有一份JAVA源码,CRC8校验功能,如何导入apicloud中调用?

  1. package com.espressif.iot.esptouch.util;

  2. import java.util.zip.Checksum;

  3. public class CRC8 implements Checksum {

  4.     private static final short[] crcTable = new short[256];
  5.     private static final short CRC_POLYNOM = 0x8c;
  6.     private static final short CRC_INITIAL = 0x00;

  7.     static {
  8.         for (int dividend = 0; dividend < 256; dividend++) {
  9.             int remainder = dividend;// << 8;
  10.             for (int bit = 0; bit < 8; ++bit)
  11.                 if ((remainder & 0x01) != 0)
  12.                     remainder = (remainder >>> 1) ^ CRC_POLYNOM;
  13.                 else
  14.                     remainder >>>= 1;
  15.             crcTable[dividend] = (short) remainder;
  16.         }
  17.     }

  18.     private final short init;
  19.     private short value;

  20.     public CRC8() {
  21.         this.value = this.init = CRC_INITIAL;
  22.     }

  23.     @Override
  24.     public void update(byte[] buffer, int offset, int len) {
  25.         for (int i = 0; i < len; i++) {
  26.             int data = buffer[offset + i] ^ value;
  27.             value = (short) (crcTable[data & 0xff] ^ (value << 8));
  28.         }
  29.     }

  30.     /**
  31.      * Updates the current checksum with the specified array of bytes.
  32.      * Equivalent to calling <code>update(buffer, 0, buffer.length)</code>.
  33.      *
  34.      * @param buffer the byte array to update the checksum with
  35.      */
  36.     public void update(byte[] buffer) {
  37.         update(buffer, 0, buffer.length);
  38.     }

  39.     @Override
  40.     public void update(int b) {
  41.         update(new byte[]{(byte) b}, 0, 1);
  42.     }

  43.     @Override
  44.     public long getValue() {
  45.         return value & 0xff;
  46.     }

  47.     @Override
  48.     public void reset() {
  49.         value = init;
  50.     }

  51. }
复制代码


52yaoer
216
帖子
5
勋章
5917
Y币
最佳答案
开发为模块插件进行调用!
216
帖子
5
勋章
5917
Y币
开发为模块插件进行调用!
33
帖子
2
勋章
5055
Y币
1.推荐服务器接口验证 (推荐) :写个接口把要验证的内容入参 进行判断
2.Android分装模块 ios 重写方法
您需要登录后才可以回帖 登录

本版积分规则