13.5、C#语言
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CRC16test {
class Program {
static int crc16(byte[] data, int size) {
int crc = 0x0;
byte data_t;
int i = 0;
int j = 0;
if (data == null) {
return 0;
}
for (j = 0; j < size; j++) {
data_t = data[j];
crc = (data_t ^ (crc));
for (i = 0;
i < 8; i++) {
if ((crc & 0x1) == 1) { crc = (crc >> 1) ^ 0xA001;
}
else {
crc >>= 1;
}
}
}
return crc;
}
static void Main(string[] args) {
int CheckValue;
string str = String.Empty; byte[] array = new byte[4]{0x33,0x20,0x9F,0xD5};
CheckValue = crc16(array, array.Length);
str = ((CheckValue >> 8) & 0xff).ToString("X2").ToUpper();
str += " "+ (CheckValue & 0xff).ToString("X2").ToUpper();
Console.WriteLine(str);
Console.ReadLine();
}
}
}