[csharp] view plaincopyprint? using System; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization.Formatters.Binary; using System.IO; namespace smsForCsharp.CRC { /// <summary> /// 消息CRC校验算法 /// </summary> pub
def crc16(x, invert): a = 0xFFFF b = 0xA001 for byte in x: a ^= ord(byte) for i in range(8): last = a % 2 a >>= 1 if last == 1: a ^= b s = hex(a).upper() return s[4:6]+s[2:4] if invert == True else s[2:4]+s[4:6] 示例: print(crc16("012345678"