bcc校验位怎么算的_用C#实现的几种常用数据校验方法整理(CRC校验;LRC校验;BCC校验;累加和校验)...

本文展示了使用C#实现的几种常见的CRC校验方法,包括CRC-4/ITU、CRC-5/EPC、CRC-5/ITU、CRC-5/USB、CRC-6/ITU、CRC-7/MMC、CRC-8/MMC、CRC-8/ITU、CRC-16/MODBUS、CRC-16/USB、CRC-16/X25、CRC-16/XMODEM、CRC32以及CRC32/MPEG-2。每个方法详细解释了校验过程并提供了相应的代码实现。
摘要由CSDN通过智能技术生成

1 ///**********************************************************************2 ///Name: CRC-4/ITU x4+x+13 ///Poly: 0x034 ///Init: 0x005 ///Refin: true6 ///Refout: true7 ///Xorout: 0x008 ///*************************************************************************

9 public static byte[] Crc1(byte[] buffer, int start = 0, int len = 0)10 {11 if (buffer == null || buffer.Length == 0) return null;12 if (start < 0) return null;13 if (len == 0) len = buffer.Length -start;14 int length = start +len;15 if (length > buffer.Length) return null;16 byte crc = 0;//Initial value

17 for (int i = start; i < length; i++)18 {19 crc ^=buffer[i];20 for (int j = 0; j < 8; j++)21 {22 if ((crc & 1) > 0)23 crc = (byte)((crc >> 1) ^ 0x0C);//0x0C = (reverse 0x03)>>(8-4)

24 else

25 crc = (byte)(crc >> 1);26 }27 }28 return new byte[] { crc };29 }30 ///**********************************************************************31 ///Name: CRC-5/EPC x5+x3+132 ///Poly: 0x0933 ///Init: 0x0934 ///Refin: false35 ///Refout: false36 ///Xorout: 0x0037 ///*************************************************************************

38 public static byte[] Crc2(byte[] buffer, int start = 0, int len = 0)39 {40 if (buffer == null || buffer.Length == 0) return null;41 if (start < 0) return null;42 if (len == 0) len = buffer.Length -start;43 int length = start +len;44 if (length > buffer.Length) return null;45 byte crc = 0x48;//Initial value: 0x48 = 0x09<

46 for (int i = start; i < length; i++)47 {48 crc ^=buffer[i];49 for (int j = 0; j < 8; j++)50 {51 if ((crc & 0x80) > 0)52 crc = (byte)((crc << 1) ^ 0x48);//0x48 = 0x09<

53 else

54 crc = (byte)(crc << 1);55 }56 }57 return new byte[] { (byte)(crc >> 3) };58 }59 ///**********************************************************************60 ///Name: CRC-5/ITU x5+x4+x2+161 ///Poly: 0x1562 ///Init: 0x0063 ///Refin: true64 ///Refout: true65 ///Xorout: 0x0066 ///*************************************************************************

67 public static byte[] Crc3(byte[] buffer, int start = 0, int len = 0)68 {69 if (buffer == null || buffer.Length == 0) return null;70 if (start < 0) return null;71 if (len == 0) len = buffer.Length -start;72 int length = start +len;73 if (length > buffer.Length) return null;74 byte crc = 0;//Initial value

75 for (int i = start; i < length; i++)76 {77 crc ^=buffer[i];78 for (int j = 0; j < 8; j++)79 {80 if ((crc & 1) > 0)81 crc = (byte)((crc >> 1) ^ 0x15);//0x15 = (reverse 0x15)>>(8-5)

82 else

83 crc = (byte)(crc >> 1);84 }85 }86 return new byte[] { crc };87 }88 ///**********************************************************************89 ///Name: CRC-5/USB x5+x2+190 ///Poly: 0x0591 ///Init: 0x1F92 ///Refin: true93 ///Refout: true94 ///Xorout: 0x1F95 ///*************************************************************************

96 public static byte[] Crc4(byte[] buffer, int start = 0, int len = 0)97 {98 if (buffer == null || buffer.Length == 0) return null;99 if (start < 0) return null;100 if (len == 0) len = buffer.Length -start;101 int length = start +len;102 if (length > buffer.Length) return null;103 byte crc = 0x1F;//Initial value

104 for (int i = start; i < length; i++)105 {106 crc ^=buffer[i];107 for (int j = 0; j < 8; j++)108 {109 if ((crc & 1) > 0)110 crc = (byte)((crc >> 1) ^ 0x14);//0x14 = (reverse 0x05)>>(8-5)

111 else

112 crc = (byte)(crc >> 1);113 }114 }115 return new byte[] {(byte)( crc ^ 0x1F) };116 }117 ///**********************************************************************118 ///Name: CRC-6/ITU x6+x+1119 ///Poly: 0x03120 ///Init: 0x00121 ///Refin: true122 ///Refout: true123 ///Xorout: 0x00124 ///*************************************************************************

125 public static byte[] Crc5(byte[] buffer, int start = 0, int len = 0)126 {127 if (buffer == null || buffer.Length == 0) return null;128 if (start < 0) return null;129 if (len == 0) len = buffer.Length -start;130 int length = start +len;131 if (length > buffer.Length) return null;132 byte crc = 0;//Initial value

133 for (int i = start; i < length; i++)134 {135 crc ^=buffer[i];136 for (int j = 0; j < 8; j++)137 {138 if ((crc & 1) > 0)139 crc = (byte)((crc >> 1) ^ 0x30);//0x30 = (reverse 0x03)>>(8-6)

140 else

141 crc = (byte)(crc >> 1);142 }143 }144 return new byte[] { crc };145 }146 ///**********************************************************************147 ///Name: CRC-7/MMC x7+x3+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值