• Crc8算法


    JavaScript实现:

    https://gitee.com/tgzhome/crc8js

    var orderbuffer = new ArrayBuffer(2)  // 计算
    var dataView= new Uint8Array(orderbuffer)
    dataView[0] = 4
    dataView[1] = 8
    var checksum = crc8.checksum(dataView)
    

      

    // "Class" for calculating CRC8 checksums...
    function CRC8(polynomial, initial_value) { // constructor takes an optional polynomial type from CRC8.POLY
      if (polynomial == null) polynomial = CRC8.POLY.CRC8_CCITT
      this.table = CRC8.generateTable(polynomial);
      this.initial_value = initial_value;
    }
    
    // Returns the 8-bit checksum given an array of byte-sized numbers
    CRC8.prototype.checksum = function(byte_array) {
      var c = this.initial_value;
    
      for (var i = 0; i < byte_array.length; i++ ) 
        c = this.table[(c ^ byte_array[i]) % 256] 
    
      return c;
    } 
    
    // returns a lookup table byte array given one of the values from CRC8.POLY 
    CRC8.generateTable =function(polynomial)
    {
      var csTable = [] // 256 max len byte array
      
      for ( var i = 0; i < 256; ++i ) {
        var curr = i
        for ( var j = 0; j < 8; ++j ) {
          if ((curr & 0x80) !== 0) {
            curr = ((curr << 1) ^ polynomial) % 256
          } else {
            curr = (curr << 1) % 256
          }
        }
        csTable[i] = curr 
      }
        
      return csTable
    }
    
    // This "enum" can be used to indicate what kind of CRC8 checksum you will be calculating
    CRC8.POLY = {
      CRC8 : 0xd5,
      CRC8_CCITT : 0x07,
      CRC8_DALLAS_MAXIM : 0x31,
      CRC8_SAE_J1850 : 0x1D,
      CRC_8_WCDMA : 0x9b,
    }
    var crc8 = new CRC8();
    module.exports = crc8;
    

      

  • 相关阅读:
    前端之CSS盒模型介绍
    前端之CSS列表及背景类属性
    前端之CSS字体和文本类属性
    流程控制语句
    前端之CSS语法及选择器
    前端之CSS创建的样式
    前端之HTML语法及常用标签
    前端之HTML样式
    网页制作之前端简介
    jQuery(一)、核心
  • 原文地址:https://www.cnblogs.com/tgzmos/p/14084253.html
Copyright © 2020-2023  润新知