public static byte[] getCrc16Code(String crcString) { string[] arr = crcString.Split(' '); string s = string.Empty; for (int i = 0; i < arr.Length; i++) { s += arr[i]; } crcString = s; // 转换成字节数组 byte[] creBytes = HexString2Bytes(crcString); // 开始crc16校验码计算 CRC16Util crc16 = new CRC16Util(); crc16.reset(); crc16.update(creBytes); int crc = crc16.getCrcValue(); // 16进制的CRC码 String crcCode = Convert.ToString(crc, 16).ToUpper(); // 补足到4位 if (crcCode.Length < 4) { // crcCode = StringUtil.lefgPadding(crcCode, '0', 4); crcCode = crcCode.PadLeft(4, '0'); } return(HexString2Bytes(crcString + crcCode)); }
/// <summary> /// 生成FCS校验值 /// </summary> /// <param name="ccc"></param> /// <returns></returns> public static byte[] makeCrc16(byte[] ccc) { CRC16Util crc16 = new CRC16Util(); crc16.reset(); crc16.update(ccc); //Console.WriteLine(RealHexToStr(crc16.getCrcValue().ToString())); byte[] test = intToByte(crc16.getCrcValue()); //log(RealHexToStr(crc16.getCrcValue().ToString())); return(test); }