|
class CRC16_Test
{
public byte[] MyCRC16_test(byte[] data)
{
byte CRC16Lo;
byte CRC16Hi;
byte CL;
byte CH;
byte SaveHi;
byte SaveLo;
int Flag;
CRC16Lo = 0xFF;
CRC16Hi = 0xFF;
CL = 0x01;
CH = 0xA0;
byte[] tmpData = data;
for (int i = 0; i < tmpData.Length; i++)
{
CRC16Lo = (byte)(CRC16Lo ^ tmpData[i]); //每一个8bit数据与CRC寄存器低8位数据进行异或
for (Flag = 0; Flag <= 7; Flag++)
{
SaveHi = CRC16Hi;
SaveLo = CRC16Lo;
CRC16Hi = (byte)(CRC16Hi >> 1); //高位右移一位
CRC16Lo = (byte)(CRC16Lo >> 1); //低位右移一位
if ((SaveHi & 0x01) == 0x01) //如果高位字节最后一位为1
{
CRC16Lo = (byte)(CRC16Lo | 0x80); //则低位字节右移后前面补1
} //否则自动补0
if ((SaveLo & 0x01) == 0x01) //如果低位字节为1,则与多项式码进行异或
{
CRC16Hi = (byte)(CRC16Hi ^ CH);
CRC16Lo = (byte)(CRC16Lo ^ CL);
}
}
}
byte[] ReturnData = new byte[2];
ReturnData[0] = CRC16Hi; //CRC高位
ReturnData[1] = CRC16Lo; //CRC低位
return ReturnData;
}
}
static void Main(string[] args)
{
CRC16_Test testbench = new CRC16_Test();
Console.WriteLine("Input A Array:");
string str = Console.ReadLine();
byte[] data1 = System.Text.Encoding.ASCII.GetBytes(str);
Console.WriteLine("Your Array is:{0}", data1);
testbench.MyCRC16_test(data1);
Console.WriteLine("Output CRC:{0}", testbench.MyCRC16_test(data1));
Console.ReadLine();
}
输入7E0005603131
输出结果:Your Array is:System.Byte[]
Output CRC:System.Byte[]
一定是我的方式不对,怎样才能看到数组里头的数据。求助~~~~~~~~~~ |
|