求CRC16源码 谷歌半天。也没找到C#版本的CRC16校验源码。至少我没用明白。我就是想把16进制数据传进去,然后给返回高字节,低字节,就OK。怎么弄啊?看网上好多应该都是正确的,就是不会用啊。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 byte[] crcCit = new byte[2];// 接收到字节数组 byte[] byteMessage = new byte[16] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; byte[] byteTime = new byte[] { byteMessage[0], byteMessage[1] ......}; crcCit = this.GetCRCCcitt(byteTime); // 校验位的判断if ((crcCit[0] == XXXXX) && (crcCit[1] == ????????)){ ...............} /// <summary> /// CRC16-CCITT检验码的取得 /// </summary> /// <param name="data"></param> /// <returns></returns> public byte[] GetCRCCcitt(byte[] data) { long crc = 0; byte i = 0x80; int j = 0; // 高位字节 string crch; // 低位字节 string crcl; byte[] bytes = new byte[2]; for (j = 0; j < data.Length; j++) { i = 0x80; while (i != 0) { if ((crc & 0x8000) != 0) { crc = crc * 2; crc = crc ^ 0x1021; } else { crc = crc * 2; } if ((data[j] & i) != 0) { crc = crc ^ 0x1021; } i = Convert.ToByte(i / 2); if (crc > 65536) { crc = crc - 65536; } } } // 取得高位字节 int intCrc = Convert.ToByte(crc / 256); crch = Convert.ToString(intCrc, 16); if (crch.Length == 1) { crch = "0" + crch; } // 取得低位字节 crcl = Convert.ToString(crc % 256, 16); if (crcl.Length == 1) { crcl = "0" + crcl; } bytes[0] = (byte)Convert.ToByte(crch.Substring(0, 2), 16); bytes[1] = (byte)Convert.ToByte(crcl.Substring(0, 2), 16); return bytes; } 楼上仁兄的我没哟个明白啊。比如我要求“05 05 05 05”的CRC16校验码等于多少。怎么做?求赐教 如果列出对象的所有属性? C#写的web程序移植问题 如何控制dataGridVeiw的删除操作 100 分 求 c# 多个客户端连接一个服务端解决方案 求教:c# 中如何使用WINPCAP 抓包 的功能呢? 求一个sql语句(比较简单) 代码重要还是实际需求重要 如何从一个二进制exe或者dll中提起出图标来?刚刚来贵地,希望大家多多照顾。 俄罗斯方块的那些四个小块的条子、块子在C#中该如何制作? C#如何实现象Java Applet一样的功能? 一道很有意思的面试题 高手帮忙,图片显示不了
byte[] byteMessage = new byte[16] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
byte[] byteTime = new byte[] { byteMessage[0], byteMessage[1] ......};
crcCit = this.GetCRCCcitt(byteTime); // 校验位的判断
if ((crcCit[0] == XXXXX) && (crcCit[1] == ????????))
{
...............
}
/// <summary>
/// CRC16-CCITT检验码的取得
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public byte[] GetCRCCcitt(byte[] data)
{
long crc = 0;
byte i = 0x80;
int j = 0; // 高位字节
string crch;
// 低位字节
string crcl; byte[] bytes = new byte[2]; for (j = 0; j < data.Length; j++)
{
i = 0x80; while (i != 0)
{
if ((crc & 0x8000) != 0)
{
crc = crc * 2;
crc = crc ^ 0x1021;
}
else
{
crc = crc * 2;
} if ((data[j] & i) != 0)
{
crc = crc ^ 0x1021;
} i = Convert.ToByte(i / 2); if (crc > 65536)
{
crc = crc - 65536;
}
}
} // 取得高位字节
int intCrc = Convert.ToByte(crc / 256);
crch = Convert.ToString(intCrc, 16);
if (crch.Length == 1)
{
crch = "0" + crch;
} // 取得低位字节
crcl = Convert.ToString(crc % 256, 16);
if (crcl.Length == 1)
{
crcl = "0" + crcl;
} bytes[0] = (byte)Convert.ToByte(crch.Substring(0, 2), 16);
bytes[1] = (byte)Convert.ToByte(crcl.Substring(0, 2), 16); return bytes;
}
求赐教