• C# CRC8


    C# CRC8

    C#
    ///
    /// This enum is used to indicate what kind of checksum you will be calculating.
    /// 
    public enum CRC8_POLY
    {
        CRC8 = 0xd5,
        CRC8_CCITT = 0x07,
        CRC8_DALLAS_MAXIM = 0x31,
        CRC8_SAE_J1850 = 0x1D,
        CRC_8_WCDMA = 0x9b,
    };
    
    /// 
    /// Class for calculating CRC8 checksums...
    /// 
    public class CRC8Calc {
        private byte[] table = new byte[256];
        
        public byte Checksum(params byte[] val ) 
        {
            if(val == null) 
                throw new ArgumentNullException("val");
                
            byte c = 0;
    
            foreach ( byte b in val ) 
            {
                c = table[c ^ b];
            }
        
            return c;
        } 
    
        public byte[] Table
        {
            get
            {
                return this.table;
            }
            set
            {
                this.table = value;
            }
        }
        
        public byte[] GenerateTable(CRC8_POLY polynomial)
        {
            byte[] csTable = new byte[256];
            
            for ( int i = 0; i < 256; ++i ) 
            {
                int curr = i;
                
                for ( int j = 0; j < 8; ++j ) 
                {
                    if ((curr & 0x80) != 0) 
                    {
                        curr = (curr << 1) ^ (int)polynomial;
                    } 
                    else 
                    {
                        curr <<= 1;
                    }
                }
                
                csTable[i] = (byte)curr;
            }
            
            return csTable;
        }
        
        public CRC8Calc(CRC8_POLY polynomial) 
        {
            this.table = this.GenerateTable(polynomial);
        }
    }
    Example
    
    C#
    using System;
    
    public class CRC8Test
    {
        public static void RunSnippet()
        {
            byte checksum;
            byte[] testVal = new byte[]
            {0xee, 0x01, 0x13, 0x00, 0x06, 0x1c, 0x00, 0x20,  0x1d, 0x00, 0x00};
            CRC8Calc crc_dallas = new CRC8Calc(CRC8_POLY.CRC8_DALLAS_MAXIM);
            checksum = crc_dallas.Checksum(testVal);
            WL(checksum);
            CRC8Calc crc = new CRC8Calc(CRC8_POLY.CRC8_CCITT);
            checksum = crc.Checksum(testVal);
            WL(checksum);
        }
        
        #region Helper methods
        
        public static void Main()
        {
            try
            {
                RunSnippet();
            }
            catch (Exception e)
            {
                string error = string.Format
                ("---\nThe following error occurred while executing 
                    the snippet:\n{0}\n---", e.ToString());
                Console.WriteLine(error);
            }
            finally
            {
                Console.Write("Press any key to continue...");
                Console.ReadKey();
            }
        }
    
        private static void WL(object text, params object[] args)
        {
            Console.WriteLine(text.ToString(), args);   
        }
        
        #endregion
    }
  • 相关阅读:
    记录下Cookie与Session
    宝塔部署 springboot 项目遇到的 一些bug处理方案
    [IDEA] [SpringBoot] 项目所写的内容不能同步到编译出的文件中
    cookie和session的区别
    JVM类加载
    线程与线程池
    子父类继承相关(static)
    界面控件开发包DevExpress 9月正式发布v21.1.6
    Delphi开发工具DevExpress VCL全新发布v21.1.5
    强大的Visual Studio插件CodeRush v21.1.7已正式发布
  • 原文地址:https://www.cnblogs.com/vipsoft/p/16240555.html
Copyright © 2020-2023  润新知