c# HexStringtoByte十六进制字符串转字节与modusCRC校验
c# HexStringtoByte十六进制字符串转字节与modusCRC校验
public static byte[] hexStrTobyte(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16); return returnBytes; }
Int32 aa = Cal_CRC_Code(returnBytes,returnBytes.Length);
public Int32 Cal_CRC_Code(byte[] ucpdata, int len) { Int32 crc = 0xffff; byte temp; int n; int len2 = 0; while (len-->0) { crc = ucpdata[len2] ^ crc; for (n = 0; n < 8; n++) { Int32 TT; TT = crc & 1;//检查最低位是否为1 crc = crc >> 1;//crc寄存器内容右移一位>>1 crc = crc & 0x7fff;//crc寄存器最高位补0,不改变其他位 if (TT == 1) {//检查最低位是否为1 crc = crc ^ 0xa001;//与多项式1010 0000 0000 0001相异或 crc = crc & 0xffff;//crc码 } } len2++; } // return SwapInt32(crc); return crc; }
string bb_string = aa.ToString("X2"); //整型转十六进制
欢迎讨论,相互学习。
cdtxw@foxmail.com