C# CRC (循环冗余校验) 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CRT_COM.CRC
{
public class CRC_
{
public static string CRC(string data)
{
byte[] byteStr = Str_change_byte(data);
ushort crc1 = CRC_GetModbus16(byteStr, byteStr.Length);
string crc_ = Convert.ToString(crc1, 16).PadLeft(4, '0').ToUpper();
return crc_;
}
public static ushort CRC_GetModbus16(byte[] pData, int nLength)
{
ushort cRc_16 = 0x0000;
byte temp;
for (int i = 0; i < nLength; ++i)
{
temp = (byte)(cRc_16 & 0xFF);
cRc_16 = (ushort)((cRc_16 >> 8) ^ g_McRctable_16[(temp ^ pData[i]) & 0xFF]);
}
return cRc_16;
}