We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 495cff8 commit c0e3642Copy full SHA for c0e3642
checksum/CRC-16.go
@@ -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