C#crc16校验和
来源:互联网 发布:淘宝上zara是真的吗 编辑:程序博客网 时间:2024/04/26 07:48
从网上搜了好久都没搞定,我老大一搜就好使,什么人品!
我的资源里有:http://download.csdn.net/detail/small5e4444/3724860
一下程序写在button事件里就行
byte[] data = new byte[6];
string[] st = textBox1.Text.Split(',');
for (int i = 0; i < st.Length; i++)
{
data[i] = Convert.ToByte(st[i]);
}
//上面是从 textbox1输入,例如 01,03,03,00,00,01
//byte[] b = new byte[] { 01, 03, 03, 0x0a, 00, 02 };//手写 注意16进制数加 ox
byte[] c = crc16(data);
// Console.WriteLine(c);
string xx = "";
for (int i = 0; i < c.Length; i++)
{
string x = Convert.ToString(c[i], 16);
xx += x;
textBox2.Text = xx;
// Console.Write(x);
// Console.Write(" ");
}
// Console.ReadLine();
}
#region CRC16校验
/// <summary>
/// CRC16校验算法,低字节在前,高字节在后
/// </summary>
/// <param name="data">要校验的数组</param>
/// <returns>返回校验结果,低字节在前,高字节在后</returns>
public static int[] crc16(int[] data)
{
if (data.Length == 0)
throw new Exception("调用CRC16校验算法,(低字节在前,高字节在后)时发生异常,异常信息:被校验的数组长度为0。");
int[] temdata = new int[data.Length + 2];
int xda, xdapoly;
int i, j, xdabit;
xda = 0xFFFF;
xdapoly = 0xA001;
for (i = 0; i < data.Length; i++)
{
xda ^= data[i];
for (j = 0; j < 8; j++)
{
xdabit = (int)(xda & 0x01);
xda >>= 1;
if (xdabit == 1)
xda ^= xdapoly;
}
}
temdata = new int[2] { (int)(xda & 0xFF), (int)(xda >> 8) };
return temdata;
}
/// <summary>
///CRC16校验算法,(低字节在前,高字节在后)
/// </summary>
/// <param name="data">要校验的数组</param>
/// <returns>返回校验结果,低字节在前,高字节在后</returns>
public static byte[] crc16(byte[] data)
{
if (data.Length == 0)
throw new Exception("调用CRC16校验算法,(低字节在前,高字节在后)时发生异常,异常信息:被校验的数组长度为0。");
byte[] temdata = new byte[data.Length + 2];
int xda, xdapoly;
byte i, j, xdabit;
xda = 0xFFFF;
xdapoly = 0xA001;
for (i = 0; i < data.Length; i++)
{
xda ^= data[i];
for (j = 0; j < 8; j++)
{
xdabit = (byte)(xda & 0x01);
xda >>= 1;
if (xdabit == 1)
xda ^= xdapoly;
}
}
temdata = new byte[2] { (byte)(xda & 0xFF), (byte)(xda >> 8) };
return temdata;
}
#endregion
- C#crc16校验和
- crc16 校验代码C语言实现 和原理分析
- Java CRC16 校验代码 和 与之对应的C代码
- CRC16校验的C代码
- CRC16校验C语言程序源码(转载)
- CRC16校验C语言程序源码(转载)
- CRC16校验C语言程序源码
- CRC16校验C语言程序源码
- CRC16校验的c语言实现
- CRC16 校验
- CRC16校验
- CRC16校验
- CRC16校验
- CRC16校验
- CRC16校验
- crc16校验
- CRC16 校验
- CRC16校验
- 网络连接方式(DAS、SAN及NAS)
- 邮件发送方法
- android Drawable Bitmap转换
- 大端小端
- 通用PHP动态生成静态HTML网页的代码
- C#crc16校验和
- progressbar 进度变色
- 使用cppunit进行白盒测试
- 美工跟程序员合作应该注意哪些问题?
- [转]块级元素和级联元素的含义
- 关于版本控制
- 看了书后,照着书上的方法一步一步去做,却失败了,555 真是的,写书的人啊,却不考虑一下细节问题 新建一个目录 [liu@liu-desktop hellomod]$mddir hellomod [liu@liu-desktop hellomod]$cd
- linux 磁盘命令详解
- JQuery 源码分析 -1