- ushort CalculateCrc16(byte[] ptr, byte len)
- {
- ushort crc;
- byte da;
- ushort[] crc_ta = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
- 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef };
-
- crc = 0;
- while (len-- != 0)
- {
- da = (byte)(crc >> 12);
- crc <<= 4;
- crc ^= crc_ta[da ^ (ptr[0] >> 4)];
- da = (byte)(crc >> 12);
- crc <<= 4;
- crc ^= crc_ta[da ^ (ptr[0] & 0x0F)];
- ptr++;
- }
- return (crc);
- }
复制代码The main changes are: - Use C# data types like ushort, byte[] instead of C++ types
- Access array elements with [ ] instead of *
- No need for explicit type casting with (byte) and (ushort)
Let me know if you have any other questions!
把下面代码用c# 语言重写一份 uint16_t CalculateCrc16(uint8_t *ptr,uint8_t len) { uint16_t crc; uint8_t da; uint16_t crc_ta[16]={ 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, }; crc=0; while(len--!=0) { da=crc>>12; crc<<=4; crc^=crc_ta[da^(*ptr>>4)]; da=crc>>12; crc<<=4; crc^=crc_ta[da^(*ptr&0x0F)]; ptr++; } return(crc); }
|