CRC-16校验C#代码

摘要:
命名空间smsForCsharp.CRC{///<16).ToUpper();
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Runtime.Serialization.Formatters.Binary;  
  5. using System.IO;  
  6.   
  7. namespace smsForCsharp.CRC  
  8. {  
  9.     /// <summary>   
  10.     /// 消息CRC校验算法   
  11.     /// </summary>   
  12.         public class CRC  
  13.         {  
  14.   
  15.             public static String getCrc16Code(String crcString)  
  16.             {  
  17.   
  18.                 // 转换成字节数组  
  19.                 byte[] creBytes = HexString2Bytes(crcString);  
  20.   
  21.                 // 开始crc16校验码计算  
  22.                 CRC16Util crc16 = new CRC16Util();  
  23.                 crc16.reset();  
  24.                 crc16.update(creBytes);  
  25.                 int crc = crc16.getCrcValue();  
  26.                 // 16进制的CRC码  
  27.                 String crcCode = Convert.ToString(crc, 16).ToUpper();  
  28.                 // 补足到4位  
  29.                 if (crcCode.Length < 4)  
  30.                 {  
  31.                     // crcCode = StringUtil.lefgPadding(crcCode, '0', 4);  
  32.                     crcCode = crcCode.PadLeft(4, '0');  
  33.                 }  
  34.                 return crcCode;  
  35.             }  
  36.   
  37.              
  38.             public static String RealHexToStr(String str)  
  39.             {  
  40.                 String hText = "0123456789ABCDEF";  
  41.                 StringBuilder bin = new StringBuilder();  
  42.                 for (int i = 0; i < str.Length; i++)  
  43.                 {  
  44.                     bin.Append(hText[str[i] / 16]).Append(hText[str[i] % 16]).Append(' ');  
  45.                 }  
  46.                 return bin.ToString();  
  47.             }  
  48.             /** 
  49.             * 十六进制字符串转换成字节数组 
  50.             *  
  51.             * @param hexstr 
  52.             * @return 
  53.             */  
  54.             public static byte[] HexString2Bytes(String hexstr)  
  55.             {  
  56.                 byte[] b = new byte[hexstr.Length / 2];  
  57.                 int j = 0;  
  58.                 for (int i = 0; i < b.Length; i++)  
  59.                 {  
  60.                     char c0 = hexstr[j++];  
  61.                     char c1 = hexstr[j++];  
  62.                     b[i] = (byte)((parse(c0) << 4) | parse(c1));  
  63.                 }  
  64.                 return b;  
  65.             }  
  66.   
  67.   
  68.             /** 
  69.             * 16进制char转换成整型 
  70.             *  
  71.             * @param c 
  72.             * @return 
  73.             */  
  74.             public static int parse(char c)  
  75.             {  
  76.                 if (c >= 'a')  
  77.                     return (c - 'a' + 10) & 0x0f;  
  78.                 if (c >= 'A')  
  79.                     return (c - 'A' + 10) & 0x0f;  
  80.                 return (c - '0') & 0x0f;  
  81.             }  
  82.   
  83.              
  84.   
  85.   
  86.             public static string ByteArrayToHexString(byte[] data)//字节数组转为十六进制字符串  
  87.             {  
  88.                 StringBuilder sb = new StringBuilder(data.Length * 3);  
  89.                 foreach (byte b in data)  
  90.                     sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));  
  91.                 return sb.ToString().ToUpper();  
  92.             }  
  93.   
  94.         }  
  95.   
  96.           
  97.   
  98.   
  99.         public class CRC16Util  
  100.         {  
  101.   
  102.             /**CRC值*/  
  103.             private int value = 0xffff;  
  104.   
  105.             private static int[] CRC16_TABLE = {  
  106.         0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,  
  107.         0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,  
  108.         0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,  
  109.         0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,  
  110.         0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,  
  111.         0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,  
  112.         0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,  
  113.         0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,  
  114.         0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,  
  115.         0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,  
  116.         0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,  
  117.         0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,  
  118.         0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,  
  119.         0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,  
  120.         0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,  
  121.         0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,  
  122.         0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,  
  123.         0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,  
  124.         0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,  
  125.         0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,  
  126.         0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,  
  127.         0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,  
  128.         0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,  
  129.         0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,  
  130.         0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,  
  131.         0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,  
  132.         0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,  
  133.         0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,  
  134.         0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,  
  135.         0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,  
  136.         0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,  
  137.         0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78  
  138.     };  
  139.   
  140.             /** 
  141.              * 计算一个字节数组的CRC值 
  142.              * @param data 
  143.              */  
  144.             public void update(byte[] data)  
  145.             {  
  146.                 //int fcs = 0xffff;  
  147.                 for (int i = 0; i < data.Length; i++)  
  148.                 {  
  149.                     // 1.value 右移8位(相当于除以256)  
  150.                     // 2.value与进来的数据进行异或运算后再与0xFF进行与运算  
  151.                     //    得到一个索引index,然后查找CRC16_TABLE表相应索引的数据  
  152.                     // 1和2得到的数据再进行异或运算。  
  153.                     value = (value >> 8) ^ CRC16_TABLE[(value ^ data[i]) & 0xff];  
  154.                 }  
  155.                 // 取反  
  156.                 //return ~fcs;  
  157.             }  
  158.   
  159.             /** 
  160.              * 计算一个byte的CRC值 
  161.              *  
  162.              * @param aByte 
  163.              */  
  164.             public void update(byte aByte)  
  165.             {  
  166.                 value = (value >> 8) ^ CRC16_TABLE[(value ^ aByte) & 0xff];  
  167.             }  
  168.   
  169.             /** 
  170.              * 重新设定CRC初始值 
  171.              */  
  172.             public void reset()  
  173.             {  
  174.                 value = 0xffff;  
  175.             }  
  176.   
  177.             /** 
  178.              * 获取计算好的CRC值 
  179.              *  
  180.              * @return 
  181.              */  
  182.             public int getCrcValue()  
  183.             {  
  184.                 return ~value & 0xffff;  
  185.             }  
  186.   
  187.             /// <summary>  
  188.             /// 生成FCS校验值  
  189.             /// </summary>  
  190.             /// <param name="ccc"></param>  
  191.             /// <returns></returns>  
  192.             public static byte[] makeCrc16(byte[] ccc)  
  193.             {  
  194.   
  195.                 CRC16Util crc16 = new CRC16Util();  
  196.                 crc16.reset();  
  197.                 crc16.update(ccc);  
  198.                 //Console.WriteLine(RealHexToStr(crc16.getCrcValue().ToString()));  
  199.                 byte[] test = intToByte(crc16.getCrcValue());  
  200.                 //log(RealHexToStr(crc16.getCrcValue().ToString()));  
  201.                 return test;  
  202.   
  203.             }  
  204.   
  205.             private static int[] copy(byte[] aa)  
  206.             {  
  207.                 int[] cc = new int[aa.Length];  
  208.                 for (int i = 0; i < aa.Length; i++)  
  209.                 {  
  210.                     cc[i] = aa[i];  
  211.                 }  
  212.                 return cc;  
  213.             }  
  214.   
  215.             public static byte[] intToByte(int i)  
  216.             {  
  217.                 byte[] abyte0 = new byte[4];  
  218.                 abyte0[0] = (byte)(0xff & i);  
  219.                 abyte0[1] = (byte)((0xff00 & i) >> 8);  
  220.                 abyte0[2] = (byte)((0xff0000 & i) >> 16);  
  221.                 abyte0[3] = (byte)((0xff000000 & i) >> 24);  
  222.                 return abyte0;  
  223.             }  
  224.   
  225.             private static void log(Object obj)  
  226.             {  
  227.                 //(obj);  
  228.             }  
  229.   
  230.             private static void printBytes(byte[] bytes)  
  231.             {  
  232.                 for (int i = 0; i < bytes.Length; i++)  
  233.                 {  
  234.                     //System.out.print(bytes[i]+ "-");  
  235.                 }  
  236.             }  
  237.   
  238.             public static String RealHexToStr(String str)  
  239.             {  
  240.                 String hText = "0123456789ABCDEF";  
  241.                 StringBuilder bin = new StringBuilder();  
  242.                 for (int i = 0; i < str.Length; i++)  
  243.                 {  
  244.                     bin.Append(hText[str[i] / 16]).Append(hText[str[i] % 16]).Append(' ');  
  245.                 }  
  246.                 return bin.ToString();  
  247.             }  
  248.   
  249.     }  
  250. }  

免责声明:文章转载自《CRC-16校验C#代码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux32_配置vncserverExcel数据提取C++代码(仅供参考)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Dijkstra和堆优化

Dijkstra算法 由于我之前一直记的迪杰斯特拉的翻译导致我把dijkstra写成了dijstra……所以下文#define dijstra dijkstra 我以后叫她迪杰克斯歘! Dijskra是用来在有向图或者无向图中寻找任意两个点的最小距离的算法。它相较于spfa不会死掉(spfa死了),但是无法处理带负环的图和求最长路(除非加上一些奇怪的东西,...

CSS中关于多个class样式设置的不同写法

html中:  <div class="containerA"> 这是AAAAAAAAAAAAAAAAAAAAAAA样式 <div class="containerB"> 这是BBBBBBBBBBBBBBBBBBBBBB样式 </div> </div>  css中: .containerA .containe...

Mybatis之批量操作

首先批量操作的优点是:大大的提高查询的效率。 举个简单的例子:如果在程序中遍历来执行sql的话,这种情况就是有多少行数据就要执行多少条sql,这样导致的效率将是非常低。 如下可能需要40s insert into USER (name,age) values ('张三','33'); insert into USER (name,age) values (...

升级IDEA后Lombok不能用了,如何解决?

今天到工作室比较晚,在电脑前吃着早饭,看到提示IDEA提示升级,寻思已经有好久没有升过级了。一样等着,就升级下吧。 升级完毕重启之后,突然发现好多错误,原来的应用也没法启动了。仔细一看报错信息,是由于Lombok相关的注解似乎都没有生效。 比如:用到@Slf4j的类里,会有类似这样的报错: java: 找不到符号 符号: 变量 log 位置:...

学习Maven之Cobertura Maven Plugin

cobertura-maven-plugin是个什么鬼? cobertura-maven-plugin是一个校验单元测试用例覆盖率的工具,可以生成一个测试覆盖率报告,可以给单元测试用例编写提供参考. helloword cobertura-maven-plugin的使用也很简单,首先你要有源码,然后要有对这个源码编写的测试代码,最后在pom.xml中配置上...

Disruptor 详解

想了解一个项目,最好的办法就是,把它的源码搞到本地自己捣鼓。   在网上看了 N 多人对 Disruptor 速度的吹捧,M 多人对它的机制分析,就连 Disruptor 官方文档中,也 NB 哄哄自诩: At LMAX we have built an order matching engine, real-time risk management, a...