crc8的js實現(crc8代碼)

  • 1、求並行 CRC8 的 verilog 代碼,多項式為 X8+X5+X4+X3+1(0x39)
  • 2、用C語言編寫,crc8校驗9個字節的數據,生成多項式為x8+x2+x+1,
  • 3、CRC校驗的算法有幾種?
  • 4、如何用java實現CRC8驗證算法
  • 5、CAN總線通信里的這個字符的CRC運算是CRC8,還是CRC16還是CRC32運算?
  • 6、nodejs 怎麼執行 crc32

在線生成工具

這個生成的是function,不可綜合的。自己改成module就行了

////////////////////////////////////////////////////////////////////////////////

// Copyright (C) 1999-2008 Easics NV.

// This source file may be used and distributed without restriction

// provided that this copyright statement is not removed from the file

// and that any derivative work contains the original copyright notice

// and the associated disclaimer.

//

// THIS SOURCE FILE IS PROVIDED “AS IS” AND WITHOUT ANY EXPRESS

// OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED

// WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

//

// Purpose : synthesizable CRC function

// * polynomial: (0 3 4 5 8)

// * data width: 8

//

// Info : tools@easics.be

//

////////////////////////////////////////////////////////////////////////////////

module CRC8_D8;

// polynomial: (0 3 4 5 8)

// data width: 8

// convention: the first serial bit is D[7]

function [7:0] nextCRC8_D8;

input [7:0] Data;

input [7:0] crc;

reg [7:0] d;

reg [7:0] c;

reg [7:0] newcrc;

begin

d = Data;

c = crc;

newcrc[0] = d[6] ^ d[5] ^ d[4] ^ d[3] ^ d[0] ^ c[0] ^ c[3] ^ c[4] ^ c[5] ^ c[6];

newcrc[1] = d[7] ^ d[6] ^ d[5] ^ d[4] ^ d[1] ^ c[1] ^ c[4] ^ c[5] ^ c[6] ^ c[7];

newcrc[2] = d[7] ^ d[6] ^ d[5] ^ d[2] ^ c[2] ^ c[5] ^ c[6] ^ c[7];

newcrc[3] = d[7] ^ d[5] ^ d[4] ^ d[0] ^ c[0] ^ c[4] ^ c[5] ^ c[7];

newcrc[4] = d[4] ^ d[3] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[3] ^ c[4];

newcrc[5] = d[6] ^ d[3] ^ d[2] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[2] ^ c[3] ^ c[6];

newcrc[6] = d[7] ^ d[4] ^ d[3] ^ d[2] ^ d[1] ^ c[1] ^ c[2] ^ c[3] ^ c[4] ^ c[7];

newcrc[7] = d[5] ^ d[4] ^ d[3] ^ d[2] ^ c[2] ^ c[3] ^ c[4] ^ c[5];

nextCRC8_D8 = newcrc;

end

endfunction

endmodule

// 8bit CRC (X(8) + X(2) + X(1) + 1)

typedef unsigned long  DWORD;

typedef unsigned short WORD;

typedef unsigned char  BYTE;

#define AL2_FCS_COEF ((1  7) + (1  6) + (1  5))

//data為指向校驗數據的指針,length為長度,返回一個字節的校驗碼

BYTE GetCrc8(unsigned char * data, int length)

{

   BYTE cFcs = 0;

   int i, j;

   for( i = 0; i  length; i ++ )

   {

      cFcs ^= data[i];

      for(j = 0; j  8; j ++)

      {

         if(cFcs  1)

         {

            cFcs = (BYTE)((cFcs  1) ^ AL2_FCS_COEF);

         }

         else

         {

            cFcs = 1;

         }

      }

   }

   return cFcs;

}

具體的找些參考資料吧,一兩句話也說不清楚

;hl=zh-CNct=clnkcd=1gl=cnst_usg=ALhdy2_AJ0YjhnCWFMb5xtYPvXTzPgd_2Q

下面的還有程序實現:

/*

*—————————————————————————

* Copyright (C) 1999,2000 Dallas Semiconductor Corporation, All Rights Reserved.

*

* Permission is hereby granted, free of charge, to any person obtaining a

* copy of this software and associated documentation files (the “Software”),

* to deal in the Software without restriction, including without limitation

* the rights to use, copy, modify, merge, publish, distribute, sublicense,

* and/or sell copies of the Software, and to permit persons to whom the

* Software is furnished to do so, subject to the following conditions:

*

* The above copyright notice and this permission notice shall be included

* in all copies or substantial portions of the Software.

*

* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS

* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

* IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES

* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

* OTHER DEALINGS IN THE SOFTWARE.

*

* Except as contained in this notice, the name of Dallas Semiconductor

* shall not be used except as stated in the Dallas Semiconductor

* Branding Policy.

*—————————————————————————

*/

package com.dalsemi.onewire.utils;

/**

* CRC8 is a class to contain an implementation of the

* Cyclic-Redundency-Check CRC8 for the iButton. The CRC8 is used

* in the 1-Wire Network address of all iButtons and 1-Wire

* devices.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @version 0.00, 28 Aug 2000

* @author DS

*

*/

public class CRC8

{

//——–

//——– Variables

//——–

/**

* CRC 8 lookup table

*/

private static byte dscrc_table [];

/*

* Create the lookup table

*/

static

{

//Translated from the assembly code in iButton Standards, page 129.

dscrc_table = new byte [256];

int acc;

int crc;

for (int i = 0; i 256; i++)

{

acc = i;

crc = 0;

for (int j = 0; j 8; j++)

{

if (((acc ^ crc) 0x01) == 0x01)

{

crc = ((crc ^ 0x18) 1) | 0x80;

}

else

crc = crc 1;

acc = acc 1;

}

dscrc_table [i] = ( byte ) crc;

}

}

//——–

//——– Constructor

//——–

/**

* Private constructor to prevent instantiation.

*/

private CRC8 ()

{

}

//——–

//——– Methods

//——–

/**

* Perform the CRC8 on the data element based on the provided seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc data element on which to perform the CRC8

* @param seed seed the CRC8 with this value

* @return CRC8 value

*/

public static int compute (int dataToCRC, int seed)

{

return (dscrc_table [(seed ^ dataToCRC) 0x0FF] 0x0FF);

}

/**

* Perform the CRC8 on the data element based on a zero seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc data element on which to perform the CRC8

* @return CRC8 value

*/

public static int compute (int dataToCRC)

{

return (dscrc_table [dataToCRC 0x0FF] 0x0FF);

}

/**

* Perform the CRC8 on an array of data elements based on a

* zero seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @return CRC8 value

*/

public static int compute (byte dataToCrc [])

{

return compute(dataToCrc, 0, dataToCrc.length);

}

/**

* Perform the CRC8 on an array of data elements based on a

* zero seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @param off offset into array

* @param len length of data to crc

* @return CRC8 value

*/

public static int compute (byte dataToCrc [], int off, int len)

{

return compute(dataToCrc, off, len, 0);

}

/**

* Perform the CRC8 on an array of data elements based on the

* provided seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @param off offset into array

* @param len length of data to crc

* @param seed seed to use for CRC8

* @return CRC8 value

*/

public static int compute (byte dataToCrc [], int off, int len, int seed)

{

// loop to do the crc on each data element

int CRC8 = seed;

for (int i = 0; i len; i++)

CRC8 = dscrc_table [(CRC8 ^ dataToCrc [i + off]) 0x0FF];

return (CRC8 0x0FF);

}

/**

* Perform the CRC8 on an array of data elements based on the

* provided seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @param seed seed to use for CRC8

* @return CRC8 value

*/

public static int compute (byte dataToCrc [], int seed)

{

return compute(dataToCrc, 0, dataToCrc.length, seed);

}

}

CAN總線的CRC場共16bit,其中最後1bit為CRC序列後隨的CRC界定符僅由單個“隱性”位構成。所以用於CRC校驗的是15bit即CRC15。用於幀校驗的CRC序列由特別適用於位數小於127位幀的循環冗餘碼校驗(BCH碼)驅動。為實現CRC計算,被除的多項式被定義為這樣一個多項式,其係數由幀起始、仲裁場、控制場、數據場(如果存在)和15位最低係數為0組成的解除填充的位流給定。此多項式被下列生成多項式

X15+X14+X10+X8+X7+X4+X3+1

除(係數按模-2計算),該多項式相除的餘數即為發至總線的CRC序列。

為實現這一功能,可以使用15位移位寄存器CRC-RG(14:0)。若NXTBIT標示由幀起始直至數據場結束的解除填充位序列給定位流的下一位,則CRC序列計算如下:

CRC—RG(14:0)=(0); //初始化移位寄存器

REPEAT

CRCNXT=NXTBIT EXOR CRC—RG(14);

CRC—RG(14:1)= CRC—RG(13:0); //左移一位

CRC—RG(0)=0;

IF CRCNXT THEN

CRC—RG(14:0)=CRC—RG(14:0)EXOR(4599H);

END IF

UNTIL(NXTBIT=數據結束或存在一個錯誤條件)。

數據場最後一位發送/接收後,CRC—RG中含有CRC序列。CRC序列後隨的CRC界定符僅由單個“隱性”位構成。

const crc=require(‘node-crc’);

var result = crc.crc32(Buffer.from(‘hello’, ‘utf8’)).toString(‘hex’);

var result2 = crc.crc64(Buffer.from(‘world’, ‘utf8’)).toString(‘hex’);

鏈接在這裡:網頁鏈接

你品,你細品

原創文章,作者:XZB0O,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/126798.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
XZB0O的頭像XZB0O
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相關推薦

  • JS Proxy(array)用法介紹

    JS Proxy(array)可以說是ES6中非常重要的一個特性,它可以代理一個數組,監聽數據變化並進行攔截、處理。在實際開發中,使用Proxy(array)可以方便地實現數據的監…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字符串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字符串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字符串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變量和數…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在着手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29
  • Python海龜代碼簡單畫圖

    本文將介紹如何使用Python的海龜庫進行簡單畫圖,並提供相關示例代碼。 一、基礎用法 使用Python的海龜庫,我們可以控制一個小海龜在窗口中移動,並利用它的“畫筆”在窗口中繪製…

    編程 2025-04-29

發表回復

登錄後才能評論