Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. type CRC16 struct {
  2. Tab []uint16
  3. Constant uint16
  4. }
  5.  
  6. // Init crc16
  7. func (c *CRC16) Init() {
  8. c.Constant = 0xA001
  9.  
  10. for i := 0; i < 256; i++ {
  11. crc := uint16(i)
  12. for j := 0; j < 8; j++ {
  13. if crc&0x0001 == 1 {
  14. crc = crc>>1 ^ c.Constant
  15. } else {
  16. crc = crc >> 1
  17. }
  18. }
  19. c.Tab = append(c.Tab, crc)
  20. }
  21. }
  22.  
  23. // Calculate CRC16
  24. func (c *CRC16) Calculate(data []byte) uint16 {
  25. var crcValue uint16 = 0x0000
  26.  
  27. for _, d := range data {
  28. tmp := crcValue ^ uint16(d)
  29. rotated := crcValue >> 8
  30. crcValue = rotated ^ c.Tab[(tmp&0x00ff)]
  31. }
  32. return crcValue
  33. }
  34.  
  35. c := CRC16{}
  36. c.Init()
  37. c.Calculate([]byte{})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement