Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type CRC16 struct {
- Tab []uint16
- Constant uint16
- }
- // Init crc16
- func (c *CRC16) Init() {
- c.Constant = 0xA001
- for i := 0; i < 256; i++ {
- crc := uint16(i)
- for j := 0; j < 8; j++ {
- if crc&0x0001 == 1 {
- crc = crc>>1 ^ c.Constant
- } else {
- crc = crc >> 1
- }
- }
- c.Tab = append(c.Tab, crc)
- }
- }
- // Calculate CRC16
- func (c *CRC16) Calculate(data []byte) uint16 {
- var crcValue uint16 = 0x0000
- for _, d := range data {
- tmp := crcValue ^ uint16(d)
- rotated := crcValue >> 8
- crcValue = rotated ^ c.Tab[(tmp&0x00ff)]
- }
- return crcValue
- }
- c := CRC16{}
- c.Init()
- c.Calculate([]byte{})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement