You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
444 B
Go

package s19
import (
"encoding/hex"
"strconv"
)
func validateChecksum(line string, checksum byte) bool {
sum := byte(0)
for i := 2; i < len(line)-2; i += 2 {
value, _ := strconv.ParseUint(line[i:i+2], 16, 8)
sum += byte(value)
}
return ^sum == checksum
}
func computeChecksum(body string) byte {
sum := byte(0)
for i := 0; i < len(body); i += 2 {
value, _ := hex.DecodeString(body[i : i+2])
sum += value[0]
}
return ^sum
}