Skip to content

Commit c0e3642

Browse files
Create CRC-16.go
add hacktoberfest label
1 parent 495cff8 commit c0e3642

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

checksum/CRC-16.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Package checksum provides functions for calculating various checksum values.
2+
package checksum
3+
4+
import "fmt"
5+
6+
// CRC16 computes the CRC-16 checksum of the input data.
7+
func CRC16(data []byte) uint16 {
8+
var crc uint16 = 0xFFFF
9+
for _, b := range data {
10+
crc ^= uint16(b)
11+
for i := 0; i < 8; i++ {
12+
if crc&0x0001 != 0 {
13+
crc = (crc >> 1) ^ 0xA001
14+
} else {
15+
crc >>= 1
16+
}
17+
}
18+
}
19+
return crc
20+
}
21+
22+
// CRC16Hex computes the CRC-16 checksum of a string and returns it as a hexadecimal string.
23+
func CRC16Hex(input string) string {
24+
crc := CRC16([]byte(input))
25+
return fmt.Sprintf("%04X", crc)
26+
}

0 commit comments

Comments
 (0)