精华内容
下载资源
问答
  • Java在与硬件通信协议中,我们需要利用帧头,长度,功能码,数据体等信息计算出CRC8校验码,就需要使用到Java封装的CRC8校验算法工具类,小编只是在项目中用到CRC校验算法(循环冗余校验算法),作为笔记保存起来,供...

    Java在与硬件通信协议中,我们需要利用帧头,长度,功能码,数据体等信息计算出CRC8校验码,就需要使用到Java封装的CRC8校验算法工具类,小编只是在项目中用到CRC校验算法(循环冗余校验算法),作为笔记保存起来,供日后使用!

    1:先导入HexUtil工具类,代码如下:public class HexUtil {

    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5',

    '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5',

    '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public static char[] encodeHex(byte[] data) {

    return encodeHex(data, true);

    }

    public static char[] encodeHex(byte[] data, boolean toLowerCase) {

    return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);

    }

    protected static char[] encodeHex(byte[] data, char[] toDigits) {

    if (data == null)

    return null;

    int l = data.length;

    char[] out = new char[l <

    for (int i = 0, j = 0; i 

    out[j++] = toDigits[(0xF0 & data[i]) >>> 4];

    out[j++] = toDigits[0x0F & data[i]];

    }

    return out;

    }

    public static String encodeHexStr(byte[] data) {

    return encodeHexStr(data, true);

    }

    public static String encodeHexStr(byte[] data, boolean toLowerCase) {

    return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);

    }

    protected static String encodeHexStr(byte[] data, char[] toDigits) {

    return new String(encodeHex(data, toDigits));

    }

    public static String formatHexString(byte[] data) {

    return formatHexString(data, false);

    }

    public static String formatHexString(byte[] data, boolean addSpace) {

    if (data == null || data.length 

    return null;

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i 

    String hex = Integer.toHexString(data[i] & 0xFF);

    if (hex.length() == 1) {

    hex = '0' + hex;

    }

    sb.append(hex);

    if (addSpace)

    sb.append(" ");

    }

    return sb.toString().trim();

    }

    public static byte[] decodeHex(char[] data) {

    int len = data.length;

    if ((len & 0x01) != 0) {

    throw new RuntimeException("Odd number of characters.");

    }

    byte[] out = new byte[len >> 1];

    // two characters form the hex value.

    for (int i = 0, j = 0; j 

    int f = toDigit(data[j], j) <

    j++;

    f = f | toDigit(data[j], j);

    j++;

    out[i] = (byte) (f & 0xFF);

    }

    return out;

    }

    protected static int toDigit(char ch, int index) {

    int digit = Character.digit(ch, 16);

    if (digit == -1) {

    throw new RuntimeException("Illegal hexadecimal character " + ch

    + " at index " + index);

    }

    return digit;

    }

    public static byte[] hexStringToBytes(String hexString) {

    if (hexString == null || hexString.equals("")) {

    return null;

    }

    hexString = hexString.toUpperCase();

    int length = hexString.length() / 2;

    char[] hexChars = hexString.toCharArray();

    byte[] d = new byte[length];

    for (int i = 0; i 

    int pos = i * 2;

    d[i] = (byte) (charToByte(hexChars[pos]) <

    }

    return d;

    }

    public static byte charToByte(char c) {

    return (byte) "0123456789ABCDEF".indexOf(c);

    }

    public static String extractData(byte[] data, int position) {

    return HexUtil.formatHexString(new byte[]{data[position]});

    }

    //使用1字节就可以表示b

    public static String numToHex8(int b) {

    return String.format("%02x", b);//2表示需要两个16进行数

    }

    //需要使用2字节表示b

    public static String numToHex16(int b) {

    return String.format("%04x", b);

    }

    //需要使用4字节表示b

    public static String numToHex32(int b) {

    return String.format("%08x", b);

    }

    public static String getBitString(byte by){

    StringBuffer sb = new StringBuffer();

    sb.append((by>>7)&0x1)

    .append((by>>6)&0x1)

    .append((by>>5)&0x1)

    .append((by>>4)&0x1)

    .append((by>>3)&0x1)

    .append((by>>2)&0x1)

    .append((by>>1)&0x1)

    .append((by>>0)&0x1);

    return sb.toString();

    }

    public static int[] getBit(byte by){

    /*StringBuffer sb = new StringBuffer();

    sb.append((by>>7)&0x1)

    .append((by>>6)&0x1)

    .append((by>>5)&0x1)

    .append((by>>4)&0x1)

    .append((by>>3)&0x1)

    .append((by>>2)&0x1)

    .append((by>>1)&0x1)

    .append((by>>0)&0x1);*/

    int[] bits= {

    (by>>7)&0x1,

    (by>>6)&0x1,

    (by>>5)&0x1,

    (by>>4)&0x1,

    (by>>3)&0x1,

    (by>>2)&0x1,

    (by>>1)&0x1,

    (by>>0)&0x1

    };

    return bits;

    }

    }

    2:在Main方法中测试CRC8算法,代码如下:public class TestDemo {

    public static void main(String[] args) {

    System.out.println(crc8("55AA021B00"));

    //输出结果为:75

    }

    public static String crc8(String hexCommond) {

    int crc8 = FindCRC(HexUtil.hexStringToBytes(hexCommond));

    return HexUtil.numToHex8(crc8).toUpperCase();

    }

    //获取CRC校验字节

    public static int FindCRC(byte[] data) {

    int CRC = 0;

    int genPoly = 0x8C;

    for (int i = 0; i 

    CRC ^= data[i];

    CRC &= 0xff;//保证CRC余码输出为1字节。

    for (int j = 0; j 

    if ((CRC & 0x01) != 0) {

    CRC = (CRC >> 1) ^ genPoly;

    CRC &= 0xff;//保证CRC余码输出为1字节。

    } else {

    CRC >>= 1;

    CRC &= 0xff;//保证CRC余码输出为1字节。

    }

    }

    }

    CRC &= 0xff;//保证CRC余码输出为1字节。

    return CRC;

    }

    }

    上述的CRC8算法根据计算出来的帧头,长度,功能码,数据体计算出来的结果为:75,与在线的crc(循环冗余校验算法)结果一致,如图所示:

    5f7f319b96f1a3f507f947bc5b1c3a78.png

    来源网站:太平洋学习网,转载请注明出处:http://www.tpyyes.com/a/java/1053.html

    展开全文
  • CRC8校验原理

    2021-03-09 20:07:15
    原理 假设CRC8校验slave address + data byte 传入两个字节的数据(16位) CRC初始值及校验key查看数据手册 不同芯片有不同的要求 如(BQ76930) ...1、进行两轮循环 每个循环校验8位 2、在for循环中对CRC

    原理

    假设CRC8校验slave address + data byte
    传入两个字节的数据(16位)
    CRC初始值及校验key查看数据手册 不同芯片有不同的要求
    如(BQ76930)

    The CRC polynomial is x8 + x2 + x + 1, and the initial value is 0.
    

    因此,初始CRC=0,多项式表示 1 0000 0111 有提到的位为1 最高位不需要参与校验 结果key为0x07

    步骤

    1、进行两轮循环 每个循环校验8位

    2、在for循环中对CRC进行8次处理,从最高位开始 如果CRC为0则左移1位。如果不为0,则CRC先左移1位,然后和key进行异或

    3、每个for中都有一次字节ptr(8位)& i 相当于ptr为掩码 从高位到低位轮询 如果为1 则CRC进行异或 否则不进行操作
    数据0x11 key0x07 结果0x77

    代码

    unsigned char CRC8(unsigned char *ptr, unsigned char len,unsigned char key)
    {
    	unsigned char i;
    	unsigned char crc=0;
    	while(len--!=0)
    	{
    		for(i=0x80; i!=0; i/=2)
    		{
    			if((crc & 0x80) != 0)
    			{
    				crc *= 2;
    				crc ^= key;
    			}
    			else
    				crc *= 2;
    
    			if((*ptr & i)!=0)
    				crc ^= key;
    		}
    		ptr++;
    	}
    	return(crc);
    }
    

    测试函数

    #include <iostream>
    #include <cstdio>
    using namespace std;
    unsigned char cal_table_high_first(unsigned char value);
    unsigned char CRC8(unsigned char *ptr, unsigned char len,unsigned char key)
    {
    	unsigned char i;
    	unsigned char crc=0;
    	while(len--!=0)
    	{
    		for(i=0x80; i!=0; i/=2)
    		{
    			if((crc & 0x80) != 0)
    			{
    				crc *= 2;
    				crc ^= key;
    			}
    			else
    				crc *= 2;
    
    			if((*ptr & i)!=0)
    				crc ^= key;
    		}
    		ptr++;
    	}
    	return(crc);
    }
    int main(){
    //四组数据,前20字节为数据位,21位为CRC校验位
    //	unsigned char str[] = {0xA8,0xFF,0xFF,0xBE,0x00,0x00,0x01,0x00,0x00,0x76,0x00,0x2A,0x20,0x2A,0x40,0x2A,0x20,0x97,0x02,0x06,0xEA};
    //	unsigned char st2[] = {0xA8,0xFF,0xFF,0xBE,0x00,0x00,0x09,0x00,0x00,0x7B,0x00,0x2A,0x00,0x2A,0x60,0x2A,0x40,0x9B,0x02,0x07,0xB1};
    //	unsigned char st3[] = {0xA8,0xFF,0xFF,0x75,0xFF,0xFF,0xDC,0x00,0x00,0x99,0x00,0x2B,0x60,0x2B,0x80,0x2B,0x60,0xAD,0x02,0x07,0x38};
    //	unsigned char st4[] = {0xA8,0xFF,0xFF,0x7E,0xFF,0xFF,0xF9,0x00,0x00,0x7A,0x00,0x2C,0xC0,0x2C,0xC0,0x2C,0xC0,0xB7,0x02,0x07,0x49};
    	unsigned char ptr = 0x11;
    	printf("0x%x ",cal_table_high_first(0x11));
    	printf("0x%x",CRC8(&ptr,1,0x07));
    	return 0;
    } 
     
    unsigned char cal_table_high_first(unsigned char value)
    {
        unsigned char i, crc;
    
        crc = value;
        /* 数据往左移了8位,需要计算8次 */
        for (i=8; i>0; --i)
        { 
            if (crc & 0x80)  /* 判断最高位是否为1 */
            {
            /* 最高位为1,不需要异或,往左移一位,然后与0x31异或 */
            /* 0x31(多项式:x8+x5+x4+1,100110001),最高位不需要异或,直接去掉 */
                crc = (crc << 1) ^ 0x07;        }
            else
            {
                /* 最高位为0时,不需要异或,整体数据往左移一位 */
                crc = (crc << 1);
            }
        }
    
        return crc;
    }
    
    展开全文
  • C的CRC8校验的源代码

    2021-05-22 18:09:17
    C的CRC8校验的源代码:// CRC8校验// X^8 + X^2 + X^1 + 1unsigned int code CRC8_TAB[256] = {0x00,0x07,0x0E,0x09,0x1C,0x1B,0x12,0x15,0x38,0x3F,0x36,0x31,0x24,0x23,0x2A,0x2D,0x70,0x77,0x7E,...

    C的CRC8校验的源代码:

    //   CRC8校验

    //   X^8   +   X^2   +   X^1   +   1

    unsigned   int   code   CRC8_TAB[256]   =   {

    0x00,0x07,0x0E,0x09,0x1C,0x1B,0x12,0x15,0x38,0x3F,0x36,0x31,0x24,0x23,0x2A,0x2D,

    0x70,0x77,0x7E,0x79,0x6C,0x6B,0x62,0x65,0x48,0x4F,0x46,0x41,0x54,0x53,0x5A,0x5D,

    0xE0,0xE7,0xEE,0xE9,0xFC,0xFB,0xF2,0xF5,0xD8,0xDF,0xD6,0xD1,0xC4,0xC3,0xCA,0xCD,

    0x90,0x97,0x9E,0x99,0x8C,0x8B,0x82,0x85,0xA8,0xAF,0xA6,0xA1,0xB4,0xB3,0xBA,0xBD,

    0xC7,0xC0,0xC9,0xCE,0xDB,0xDC,0xD5,0xD2,0xFF,0xF8,0xF1,0xF6,0xE3,0xE4,0xED,0xEA,

    0xB7,0xB0,0xB9,0xBE,0xAB,0xAC,0xA5,0xA2,0x8F,0x88,0x81,0x86,0x93,0x94,0x9D,0x9A,

    0x27,0x20,0x29,0x2E,0x3B,0x3C,0x35,0x32,0x1F,0x18,0x11,0x16,0x03,0x04,0x0D,0x0A,

    0x57,0x50,0x59,0x5E,0x4B,0x4C,0x45,0x42,0x6F,0x68,0x61,0x66,0x73,0x74,0x7D,0x7A,

    0x89,0x8E,0x87,0x80,0x95,0x92,0x9B,0x9C,0xB1,0xB6,0xBF,0xB8,0xAD,0xAA,0xA3,0xA4,

    0xF9,0xFE,0xF7,0xF0,0xE5,0xE2,0xEB,0xEC,0xC1,0xC6,0xCF,0xC8,0xDD,0xDA,0xD3,0xD4,

    0x69,0x6E,0x67,0x60,0x75,0x72,0x7B,0x7C,0x51,0x56,0x5F,0x58,0x4D,0x4A,0x43,0x44,

    0x19,0x1E,0x17,0x10,0x05,0x02,0x0B,0x0C,0x21,0x26,0x2F,0x28,0x3D,0x3A,0x33,0x34,

    0x4E,0x49,0x40,0x47,0x52,0x55,0x5C,0x5B,0x76,0x71,0x78,0x7F,0x6A,0x6D,0x64,0x63,

    0x3E,0x39,0x30,0x37,0x22,0x25,0x2C,0x2B,0x06,0x01,0x08,0x0F,0x1A,0x1D,0x14,0x13,

    0xAE,0xA9,0xA0,0xA7,0xB2,0xB5,0xBC,0xBB,0x96,0x91,0x98,0x9F,0x8A,0x8D,0x84,0x83,

    0xDE,0xD9,0xD0,0xD7,0xC2,0xC5,0xCC,0xCB,0xE6,0xE1,0xE8,0xEF,0xFA,0xFD,0xF4,0xF3

    };

    unsigned   int   CRC8_Tab(unsigned   char   xdata   *   ucPtr,   unsigned   char   ucLen)

    {

    unsigned   char   ucIndex;                 //   CRC8校验表格索引

    unsigned   char   ucCRC8   =   0;                 //   CRC8字节初始化

    //   进行CRC8位校验

    while   (ucLen   --){

    ucIndex   =   ucCRC8   ^   (*   ucPtr   ++);

    ucCRC8   =   CRC8_TAB[ucIndex];

    }

    //   返回CRC8校验数据

    return   (~ucCRC8);

    }

    unsigned int FindCRC(string &data,unsigned int datalen)

    {

    unsigned int CRC=0;

    unsigned int genPoly = 0x07;

    for (int i=0; i

    {

    CRC ^= data;

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

    {

    if(CRC & 0x80 )

    CRC = (CRC << 1) ^ genPoly;

    else

    CRC <<= 1;

    }

    }

    CRC &= 0xff;//保证CRC余码输出为2字节。

    return CRC;

    }

    展开全文
  • 1、计算法实现校验1 uint8 CRC8( uint8* pBuffer, uint16 bufSize )2 {3 uint8 crc = 0;4 // 数据合法5 if ( bufSize <= 0 )6 {7 return crc;8 }9 while( bufSize-- != 0 )10 {11 for ( uint16 i = 0x8...

    1、计算法实现校验

    1 uint8 CRC8( uint8* pBuffer, uint16 bufSize )

    2 {

    3   uint8 crc = 0;

    4   // 数据合法

    5   if ( bufSize <= 0 )

    6   {

    7     return crc;

    8   }

    9   while( bufSize-- != 0 )

    10   {

    11     for ( uint16 i = 0x80; i != 0; i /= 2 )

    12     {

    13       if ( (crc & 0x80) != 0)

    14       {

    15         crc *= 2;

    16         crc ^= 0x07; // 多项式:X8 + X2 + X + 1

    17       }

    18       else

    19       {

    20         crc *= 2;

    21       }

    22       if ( (*pBuffer & i) != 0 )

    23       {

    24         crc ^= 0x07;

    25       }

    26    }

    27    pBuffer++;

    28   }

    29 return crc;

    30 }

    2、查表法实现校验

    java代码

    1 package com.example;

    2

    3 public class CRC8Util {

    4

    5 /**

    6 * CRC8 校验 多项式 x8+x2+x+1

    7 *

    8 * @param b

    9 * @param len

    10 * @return

    11 */

    12 public static byte getCrc8(final byte[] b, final int len) {

    13 return getCrc8(b, 0, len);

    14 }

    15

    16 public static byte getCrc8(final byte[] b, final int off, final int len) {

    17 byte result = (byte) 0xFF;

    18 for (int i = 0; i < len; i++) {

    19 result = CHECKSUM_TABLE[(result ^ (b[i + off] & 0xFF)) & 0xFF];

    20 }

    21 return (byte) (result & 0xFFL);

    22 }

    23

    24 private final static byte[] CHECKSUM_TABLE = { (byte) 0x00, (byte) 0x07, (byte) 0x0e, (byte) 0x09, (byte) 0x1c,

    25 (byte) 0x1b, (byte) 0x12, (byte) 0x15, (byte) 0x38, (byte) 0x3f, (byte) 0x36, (byte) 0x31, (byte) 0x24,

    26 (byte) 0x23, (byte) 0x2a, (byte) 0x2d, (byte) 0x70, (byte) 0x77, (byte) 0x7e, (byte) 0x79, (byte) 0x6c,

    27 (byte) 0x6b, (byte) 0x62, (byte) 0x65, (byte) 0x48, (byte) 0x4f, (byte) 0x46, (byte) 0x41, (byte) 0x54,

    28 (byte) 0x53, (byte) 0x5a, (byte) 0x5d, (byte) 0xe0, (byte) 0xe7, (byte) 0xee, (byte) 0xe9, (byte) 0xfc,

    29 (byte) 0xfb, (byte) 0xf2, (byte) 0xf5, (byte) 0xd8, (byte) 0xdf, (byte) 0xd6, (byte) 0xd1, (byte) 0xc4,

    30 (byte) 0xc3, (byte) 0xca, (byte) 0xcd, (byte) 0x90, (byte) 0x97, (byte) 0x9e, (byte) 0x99, (byte) 0x8c,

    31 (byte) 0x8b, (byte) 0x82, (byte) 0x85, (byte) 0xa8, (byte) 0xaf, (byte) 0xa6, (byte) 0xa1, (byte) 0xb4,

    32 (byte) 0xb3, (byte) 0xba, (byte) 0xbd, (byte) 0xc7, (byte) 0xc0, (byte) 0xc9, (byte) 0xce, (byte) 0xdb,

    33 (byte) 0xdc, (byte) 0xd5, (byte) 0xd2, (byte) 0xff, (byte) 0xf8, (byte) 0xf1, (byte) 0xf6, (byte) 0xe3,

    34 (byte) 0xe4, (byte) 0xed, (byte) 0xea, (byte) 0xb7, (byte) 0xb0, (byte) 0xb9, (byte) 0xbe, (byte) 0xab,

    35 (byte) 0xac, (byte) 0xa5, (byte) 0xa2, (byte) 0x8f, (byte) 0x88, (byte) 0x81, (byte) 0x86, (byte) 0x93,

    36 (byte) 0x94, (byte) 0x9d, (byte) 0x9a, (byte) 0x27, (byte) 0x20, (byte) 0x29, (byte) 0x2e, (byte) 0x3b,

    37 (byte) 0x3c, (byte) 0x35, (byte) 0x32, (byte) 0x1f, (byte) 0x18, (byte) 0x11, (byte) 0x16, (byte) 0x03,

    38 (byte) 0x04, (byte) 0x0d, (byte) 0x0a, (byte) 0x57, (byte) 0x50, (byte) 0x59, (byte) 0x5e, (byte) 0x4b,

    39 (byte) 0x4c, (byte) 0x45, (byte) 0x42, (byte) 0x6f, (byte) 0x68, (byte) 0x61, (byte) 0x66, (byte) 0x73,

    40 (byte) 0x74, (byte) 0x7d, (byte) 0x7a, (byte) 0x89, (byte) 0x8e, (byte) 0x87, (byte) 0x80, (byte) 0x95,

    41 (byte) 0x92, (byte) 0x9b, (byte) 0x9c, (byte) 0xb1, (byte) 0xb6, (byte) 0xbf, (byte) 0xb8, (byte) 0xad,

    42 (byte) 0xaa, (byte) 0xa3, (byte) 0xa4, (byte) 0xf9, (byte) 0xfe, (byte) 0xf7, (byte) 0xf0, (byte) 0xe5,

    43 (byte) 0xe2, (byte) 0xeb, (byte) 0xec, (byte) 0xc1, (byte) 0xc6, (byte) 0xcf, (byte) 0xc8, (byte) 0xdd,

    44 (byte) 0xda, (byte) 0xd3, (byte) 0xd4, (byte) 0x69, (byte) 0x6e, (byte) 0x67, (byte) 0x60, (byte) 0x75,

    45 (byte) 0x72, (byte) 0x7b, (byte) 0x7c, (byte) 0x51, (byte) 0x56, (byte) 0x5f, (byte) 0x58, (byte) 0x4d,

    46 (byte) 0x4a, (byte) 0x43, (byte) 0x44, (byte) 0x19, (byte) 0x1e, (byte) 0x17, (byte) 0x10, (byte) 0x05,

    47 (byte) 0x02, (byte) 0x0b, (byte) 0x0c, (byte) 0x21, (byte) 0x26, (byte) 0x2f, (byte) 0x28, (byte) 0x3d,

    48 (byte) 0x3a, (byte) 0x33, (byte) 0x34, (byte) 0x4e, (byte) 0x49, (byte) 0x40, (byte) 0x47, (byte) 0x52,

    49 (byte) 0x55, (byte) 0x5c, (byte) 0x5b, (byte) 0x76, (byte) 0x71, (byte) 0x78, (byte) 0x7f, (byte) 0x6a,

    50 (byte) 0x6d, (byte) 0x64, (byte) 0x63, (byte) 0x3e, (byte) 0x39, (byte) 0x30, (byte) 0x37, (byte) 0x22,

    51 (byte) 0x25, (byte) 0x2c, (byte) 0x2b, (byte) 0x06, (byte) 0x01, (byte) 0x08, (byte) 0x0f, (byte) 0x1a,

    52 (byte) 0x1d, (byte) 0x14, (byte) 0x13, (byte) 0xae, (byte) 0xa9, (byte) 0xa0, (byte) 0xa7, (byte) 0xb2,

    53 (byte) 0xb5, (byte) 0xbc, (byte) 0xbb, (byte) 0x96, (byte) 0x91, (byte) 0x98, (byte) 0x9f, (byte) 0x8a,

    54 (byte) 0x8d, (byte) 0x84, (byte) 0x83, (byte) 0xde, (byte) 0xd9, (byte) 0xd0, (byte) 0xd7, (byte) 0xc2,

    55 (byte) 0xc5, (byte) 0xcc, (byte) 0xcb, (byte) 0xe6, (byte) 0xe1, (byte) 0xe8, (byte) 0xef, (byte) 0xfa,

    56 (byte) 0xfd, (byte) 0xf4, (byte) 0xf3 };

    57

    58 }

    C代码:

    1 const uint8_t CRC8[] = {

    2 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,

    3 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,

    4 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,

    5 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,

    6 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,

    7 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,

    8 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,

    9 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,

    10 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,

    11 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,

    12 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,

    13 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,

    14 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,

    15 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,

    16 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,

    17 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3

    18 };

    19 uint8_t GetCRC8(uint8_t *pBuf, uint16_t Len)

    20 {

    21 uint8_t CRC_Temp;

    22 uint8_t CRC_Index;

    23 uint16_t i = 0;

    24 CRC_Temp = 0;

    25 for (i=0; i

    26 CRC_Index = CRC_Temp ^ pBuf[i];

    27 CRC_Temp = CRC8[CRC_Index];

    28 }

    29 return CRC_Temp;

    30 }

    标签:CRC8,uint8,len,X8,result,X2,byte,final

    来源: https://www.cnblogs.com/ershoushouji/p/14301557.html

    展开全文
  • 物联网 服务器需要检验设备传输的数据,设备c语言写的,CRC8 校验多项式为x8+x2+x+1,找了百度,必应。也没有找到java CRC8多项式 x8+x2+x+1的实现,能找到的都是x8+x5+x4+1的,后转google,费了些功夫不过还好找到...
  • 【废话一段】你说这JAVA,可说是世界上用的人非常之多的一种语言,那么,为什么找一段CRC8校验的实现方式那么难???继续废话,也还是项目,我需要一个CRC8校验的模块,我承认我懒,因此我直接X度,本以为轻轻松松...
  • CRC8查表法2009-02-05 10:09/*****************************************************描 述: CRC8校验子程序 x^8+x^5+x^4+x^0入口参数: 指向数组指针,校验字节个数出口参数: 8CRC校验码**********************...
  • public class CCRC8_3 {/*public static int[] CRC8_TAB = { 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12,0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77,0x7E, 0x79, 0x6C, 0x6...
  • 计算法 CRC8校验

    2021-03-20 15:55:36
    计算法 CRC8校验 unsigned char crc8(unsigned char *vptr,unsigned char len) { const unsigned char *data=vptr; unsigned char crc=0; int i,j; for(j=len;j;j–,data++) { crc ^=(*data<<8); for(i=8;i;i...
  • CDT协议中crc8校验算法

    2021-05-04 16:57:37
    本文计算方法均参考他人文档...void crc8_CDT_init(uint32_t poly) { uint16_t crc; for (int i = 0, j; i < 256; i++) { crc = 0x00FF & i; for (j = 0; j < 8; j++) { crc = 0x00FF & (((crc ..
  • Java CRC8校验 | 学步园

    2021-02-27 18:50:49
    Java版本的CRC8校验,查表法,效率不会很差吧package com.ynsoft.java.comunication.utils;/*** CRC8相关计算* encode: utf-8** @author trb* @date 2013-11-21*/public class CRC8 {static byte[] crc8_tab = { ...
  • CDT规约之CRC8校验

    2021-09-10 20:55:49
    CDT规约中校验码的计算是用的CRC校验。 CRC(Cyclic Redundancy Check)即循环冗余校验码,是一种很常用的校验码。它有很强的检错、纠错能力,并且实现编码和检码的电路比较简单,常用于串行传送中。 CRC的编码思想...
  • 用到了modbus的校验,由于不需要modbus庞大的程序,所以需要CRC16校验程序感谢正点原子的分享//CRC高位字节值表const uint8_t auchCRCHi[] = {0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41...
  • 本文主要讲述西门子smart 200PLC查表法计算CRC8校验码,既然是查表法,首先就是输入表数据,怎样快速的在PLC里建表呢?,下面直接上图: 200PLC里建表,大家可以利用数据块,输入表数据,至于表是怎么产生的,大家...
  • 第一个函数是用于计算crc8的值,第二个函数是对整个table的结果进行校验,会先删除掉最后的crc值 然后计算crc并且和被删除的crc进行比较,如果返回原来的table则证明 crc校验通过,否则crc 校验不通过。 -- caculate...
  • CRC8算法表有两种形式,第一种是256个字节的表,第二种是高4位16字节,低4位16字节。 根据不同多项式算出对应的表中的数据 第一种方法:
  • CRC8校验

    2021-12-07 18:05:31
    其他参考: CRC校验的查表法理解_boom的博客-CSDN博客_crc校验查表 CRC8/CRC16/CRC32最总结 CRC8: (转)CRC8查表法_tyrone_新浪博客 crc8校验查表法实现方法 - 百度文库 CRC8查表法中表的具体计算过程_四点能的博客...
  • public static int getCrc(byte[] data){ int CRC = 0; int genPoly = 0x8C; for(int i = 0; i < data.length; i++){ CRC ^= data[i];... 8; j++){ if((CRC & 0x01) != 0){ CRC = (CRC
  • crc8校验查表法实现方法/* CRC-8 for Dallas iButton products From Maxim/Dallas AP Note 27 "Understanding and Using Cyclic Redundancy Checks with Dallas Semiconductor iButton Products" The Ap note...
  • /*CRC-8 for Dallas iButton productsFrom Maxim/Dallas AP Note 27"Understanding and Using Cyclic Redundancy Checks withDallas Semiconductor iButton Products"The Ap note describes the CRC-8 algorithm use...
  • CRC校验类型:CRC8/MAXIM多项式:X8+X5+X4+1Poly:0011 0001 0x31高位放到后面就变成 1000 1100 0x8cC现实代码:unsigned char crc8_chk_value(unsigned char *message, unsigned char len){uint8 crc;uint8 i;crc =...
  • 1、 CRC8标准生成多项式CRC-8 x8+x5+x4+1 0x31(0x131)CRC-8 x8+x2+x1+1 0x07(0x107)CRC-8 x8+x6+x4+x3+x2+x1 0x5E(0x15E)注:1.由于多项式的最高为都为1,并且在代码的crc8计算中,最高位也是不使用的,所以在多...
  • JS实现CRC校验

    2021-09-17 14:01:37
    var crc_ret = 0; // JS计算出来的CRC结果 var crc32_table = new Array(); var crc_generate_one_flag = 0; //如果是负数,0变1,1变0 function exchange(str){ var arr = str.split(''); for(var i = 0; i...
  • CRC_8循环冗余校验码verilog实现前言CRC原理概述过程引用 前言 CRC原理 概述 为了检测数据传输是否准确,人们发明了许多检测方式,常见的有奇偶校验、因特网校验和循环冗余校验等。 循环冗余码的本质就是在需要发送...
  • 这里使用Byte数组进行传输,前后台对协议进行商定,而后编码、传输、解码实现通信,传输过程中,为保证传输内容的完整性,需要使用CRC进行校验CRC概念——百度百科CRC即循环冗余校验码(Cyclic Redundancy Check[1]...
  • CRC8查表法

    千次阅读 2021-02-05 13:52:45
    /*****************************************************描 述: CRC8校验子程序 x^8+x^5+x^4+x^0入口参数: 指向数组指针,校验字节个数出口参数: 8CRC校验码************************************************...
  • CRC校验原理及代码实现目录背景原理模2除法多项式计算流程代码实现()c语言实现c语言测试结果labview实现labview测试结果 目录 背景 在进行数据传输时,为了避免数据传输发生错误,需要对数据进行校验,从而产生了...
  • golang实现CRC8算法

    2020-12-23 18:06:50
    CRC校验算法,就是把需要校验的数据与多项式进行循环异或(XOR),但进行异或的方式与实际中数据传输时,是高位先传、还是低位先传有关。对于数据高位先传的方式,异或从数据的高位开始,我们暂且称它顺序异或;对于...
  • * * @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 */ ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 58,312
精华内容 23,324
关键字:

crc8校验