C#写的CRC16检验算法
- /// <summary>
- /// CRC校验
- /// </summary>
- public class CRC
- {
- #region CRC16
- public static byte[] CRC16(byte[] data)
- {
- int len = data.Length;
- if (len > 0)
- {
- ushort crc = 0xFFFF;
- for (int i = 0; i < len; i++)
- {
- crc = (ushort)(crc ^ (data[i]));
- for (int j = 0; j < 8; j++)
- {
- crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
- }
- }
- byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
- byte lo = (byte)(crc & 0x00FF); //低位置
- return new byte[] { hi, lo };
- }
- return new byte[] { 0, 0 };
- }
- #endregion
- #region ToCRC16
- public static string ToCRC16(string content)
- {
- return ToCRC16(content, Encoding.UTF8);
- }
- public static string ToCRC16(string content, bool isReverse)
- {
- return ToCRC16(content, Encoding.UTF8, isReverse);
- }
- public static string ToCRC16(string content, Encoding encoding)
- {
- return ByteToString(CRC16(encoding.GetBytes(content)), true);
- }
- public static string ToCRC16(string content, Encoding encoding, bool isReverse)
- {
- return ByteToString(CRC16(encoding.GetBytes(content)), isReverse);
- }
- public static string ToCRC16(byte[] data)
- {
- return ByteToString(CRC16(data), true);
- }
- public static string ToCRC16(byte[] data, bool isReverse)
- {
- return ByteToString(CRC16(data), isReverse);
- }
- #endregion
- #region ToModbusCRC16
- public static string ToModbusCRC16(string s)
- {
- return ToModbusCRC16(s, true);
- }
- public static string ToModbusCRC16(string s, bool isReverse)
- {
- return ByteToString(CRC16(StringToHexByte(s)), isReverse);
- }
- public static string ToModbusCRC16(byte[] data)
- {
- return ToModbusCRC16(data, true);
- }
- public static string ToModbusCRC16(byte[] data, bool isReverse)
- {
- return ByteToString(CRC16(data), isReverse);
- }
- #endregion
- #region ByteToString
- public static string ByteToString(byte[] arr, bool isReverse)
- {
- try
- {
- byte hi = arr[0], lo = arr[1];
- return Convert.ToString(isReverse ? hi + lo * 0x100 : hi * 0x100 + lo, 16).ToUpper().PadLeft(4, '0');
- }
- catch (Exception ex) { throw (ex); }
- }
- public static string ByteToString(byte[] arr)
- {
- try
- {
- return ByteToString(arr, true);
- }
- catch (Exception ex) { throw (ex); }
- }
- #endregion
- #region StringToHexString
- public static string StringToHexString(string str)
- {
- StringBuilder s = new StringBuilder();
- foreach (short c in str.ToCharArray())
- {
- s.Append(c.ToString("X4"));
- }
- return s.ToString();
- }
- #endregion
- #region StringToHexByte
- private static string ConvertChinese(string str)
- {
- StringBuilder s = new StringBuilder();
- foreach (short c in str.ToCharArray())
- {
- if (c <= 0 || c >= 127)
- {
- s.Append(c.ToString("X4"));
- }
- else
- {
- s.Append((char)c);
- }
- }
- return s.ToString();
- }
- private static string FilterChinese(string str)
- {
- StringBuilder s = new StringBuilder();
- foreach (short c in str.ToCharArray())
- {
- if (c > 0 && c < 127)
- {
- s.Append((char)c);
- }
- }
- return s.ToString();
- }
- /// <summary>
- /// 字符串转16进制字符数组
- /// </summary>
- /// <param name="hex"></param>
- /// <returns></returns>
- public static byte[] StringToHexByte(string str)
- {
- return StringToHexByte(str, false);
- }
- /// <summary>
- /// 字符串转16进制字符数组
- /// </summary>
- /// <param name="str"></param>
- /// <param name="isFilterChinese">是否过滤掉中文字符</param>
- /// <returns></returns>
- public static byte[] StringToHexByte(string str, bool isFilterChinese)
- {
- string hex = isFilterChinese ? FilterChinese(str) : ConvertChinese(str);
- //清除所有空格
- hex = hex.Replace(" ", "");
- //若字符个数为奇数,补一个0
- hex += hex.Length % 2 != 0 ? "0" : "";
- byte[] result = new byte[hex.Length / 2];
- for (int i = 0, c = result.Length; i < c; i++)
- {
- result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
- }
- return result;
- }
- #endregion
- }
调用示例:
CRC.ToCRC16("012345678", true); //结果为:C3CD
CRC.ToCRC16("012345678", false); //结果为:CDC3
CRC.ToModbusCRC16("012345678", true); //结果为:2801
CRC.ToCRC16("你好,我们测试一下CRC16算法", true); //结果为:0182
C#写的CRC16检验算法的更多相关文章
- JS写的CRC16校验算法
var CRC = {}; CRC.CRC16 = function (data) { var len = data.length; if (len > 0) { var crc = 0xFFF ...
- JS写的CRC16校验算法(查表法)
var CRC = {}; CRC._auchCRCHi = [ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0 ...
- TensorFlow 入门之手写识别(MNIST) softmax算法
TensorFlow 入门之手写识别(MNIST) softmax算法 MNIST flyu6 softmax回归 softmax回归算法 TensorFlow实现softmax softmax回归算 ...
- 『练手』手写一个独立Json算法 JsonHelper
背景: > 一直使用 Newtonsoft.Json.dll 也算挺稳定的. > 但这个框架也挺闹心的: > 1.影响编译失败:https://www.cnblogs.com/zih ...
- BitSet: 有1千万个随机数,随机数的范围在1到1亿之间。现在要求写出一种算法,将1到1亿之间没有在随机数中的数求出来?
package common; import java.util.ArrayList; import java.util.BitSet; import java.util.List; import j ...
- Algorithm 用各种语言写出n!的算法
写出n!的算法 C# 递归方式: class Program { static void Main(string[] args) { Console.WriteLine("请输入一个数!&q ...
- TensorFlow 入门之手写识别(MNIST) softmax算法 二
TensorFlow 入门之手写识别(MNIST) softmax算法 二 MNIST Fly softmax回归 softmax回归算法 TensorFlow实现softmax softmax回归算 ...
- C++与JAVA代码实现CRC-16/MODBUS算法,且与 http://www.ip33.com/crc.html 进行结果验证
CRC-16/MODBUS的多项式为:x16+x15+x2+1(8005),宽度为16.运算时,首先将一个16位的寄存器预置为11111111 11111111,然后连续把数据帧中的每个字节中的8位与 ...
- Python 手写数字识别-knn算法应用
在上一篇博文中,我们对KNN算法思想及流程有了初步的了解,KNN是采用测量不同特征值之间的距离方法进行分类,也就是说对于每个样本数据,需要和训练集中的所有数据进行欧氏距离计算.这里简述KNN算法的特点 ...
随机推荐
- Nico Nico Ni~(完全背包)
Time Limit:2000MS Memory Limit:65535K Type: Program Language: Not Limited Description Lys plays L ...
- ASP.NET 操作Cookie详解 增加,修改,删除
Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份而储存在用户本地终端上的数据(通常经过加密).定义于RFC2109.它是网景公司的前雇员Lou Montulli在1993年3 ...
- 解决HttpServletResponse输出的中文乱码问题
http://blog.csdn.net/simon_1/article/details/9092747 首先,response返回有两种,一种是字节流outputstream,一种是字符流print ...
- 纯CSS 实现tooltip 内容提示信息效果
Tooltip 也就是内容的提示信息,合理使用可以给用户比较好的体验. 实现方法有很多种,有很多JS 插件,我这里介绍的是纯CSS实现的方法,兼容性也比较靠谱,IE8+均可正常显示.实现方法也非常简单 ...
- c++ 关于new文件
new文件用来管理c++的动态内存,这个文件声明了几个全局空间的函数(不是std空间的函数,全局空间的函数调用时是用全局作用域解析符),包括operator new 和operator delete的 ...
- 转:关于垂直网格与CSS基线对其的探讨
网页设计布局中一直比较流行网格对齐,但只是针对水平的对齐,很少或者没有涉及垂直对齐,这篇文章很详细的讲解了垂直网格,乃至基线对其的相关,而css3中的多列布局的也使其显得更为重要,因此还是很有必要去了 ...
- Xcode的command+shift+o是一个不错的工具
一,经历 1.在向UITextField中输入图片的时候,可以使用 NSAttributedString 添加,但是很难找到能够返回NSAttributedString对象的方法. 2.通过comma ...
- 李洪强漫谈iOS开发[C语言]-045-循环结构
- ./*** > /dev/null 2>&1
转载:http://dongwei.iteye.com/blog/322702 shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义 ...
- oracle 连接查询,和(+)符号的用法
--连接查询 左链接.右链接,全链接 --内链接select e.account 用户名, e.empname 名称, c.comname 公司名称 from employee e inner jo ...