『回复列表(10|显示机器人聊天)』
[email protected]
package main
//计算hex校验和
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
func main() {
fmt.Println("请输入要计算校验和的行:")
reader := bufio.NewReader(os.Stdin)
line, := reader.ReadString('\n')
line = strings.TrimSpace(line)
checksum, := strconv.ParseInt(line[len(line)-2:], 16, 64) //
pattern := regexp.MustCompile(`.{2}`)
parts := pattern.FindAllString(line[1:len(line)-2], -1)
var nums []int
for _, p := range parts {
num, _ := strconv.ParseInt(p, 16, 64)
nums = append(nums, int(num))
}
newChecksum := 256 - reduce(nums)
if checksum == int64(newChecksum) {
fmt.Println("校验和匹配,该行无需修改")
} else {
fmt.Printf("校验和不匹配,原校验和:%d,应该改为:%d\n", checksum, newChecksum)
fmt.Println("正确的行:")
fmt.Printf("%s%02X\n", line[:len(line)-2], newChecksum)
}
}
func reduce(nums []int) int {
return reduceHelper(nums, 0)
}
func reduceHelper(nums []int, acc int) int {
if len(nums) == 0 {
return acc % 256
}
return reduceHelper(nums[1:], (acc+nums[0])%256)
}
This is a program written in Go to calculate the checksum of a given hex line and compare it to the last two digits of the input line. The program takes input from the user, removes whitespace characters, and extracts the last two digits as the expected checksum. Then, it extracts pairs of hex digits (two characters) from the remaining part of the input line and converts them to integers. The reduce function is called with the resulting list of integers as an argument, which recursively sums them and takes the result modulo 256. The calculated checksum is then compared to the expected checksum, and if they match, the program outputs that the line is correct. Otherwise, the program outputs the correct checksum and the modified line with the correct checksum appended at the end.
[email protected]
package main
//计算hex校验和
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
func main() {
fmt.Println("请输入要计算校验和的行:")
reader := bufio.NewReader(os.Stdin)
line, := reader.ReadString('\n')
line = strings.TrimSpace(line)
checksum, := strconv.ParseInt(line[len(line)-2:], 16, 64) //
pattern := regexp.MustCompile(`.{2}`)
parts := pattern.FindAllString(line[1:len(line)-2], -1)
var nums []int
for _, p := range parts {
num, _ := strconv.ParseInt(p, 16, 64)
nums = append(nums, int(num))
}
newChecksum := 256 - reduce(nums)
if checksum == int64(newChecksum) {
fmt.Println("校验和匹配,该行无需修改")
} else {
fmt.Printf("校验和不匹配,原校验和:%d,应该改为:%d\n", checksum, newChecksum)
fmt.Println("正确的行:")
fmt.Printf("%s%02X\n", line[:len(line)-2], newChecksum)
}
}
func reduce(nums []int) int {
return reduceHelper(nums, 0)
}
func reduceHelper(nums []int, acc int) int {
if len(nums) == 0 {
return acc % 256
}
return reduceHelper(nums[1:], (acc+nums[0])%256)
}
改下添加按任意键退出
Sure, you can modify the code to add a prompt for the user to press any key to exit the program. You can use the function to wait for the user to input any text before exiting the program. Here's an example:fmt.Scanln()
go
Copy code
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
func main() {
fmt.Println("请输入要计算校验和的行:")
reader := bufio.NewReader(os.Stdin)
line, := reader.ReadString('\n')
line = strings.TrimSpace(line)
checksum, := strconv.ParseInt(line[len(line)-2:], 16, 64)
pattern := regexp.MustCompile(`.{2}`)
parts := pattern.FindAllString(line[1:len(line)-2], -1)
var nums []int
for _, p := range parts {
num, _ := strconv.ParseInt(p, 16, 64)
nums = append(nums, int(num))
}
newChecksum := 256 - reduce(nums)
if checksum == int64(newChecksum) {
fmt.Println("校验和匹配,该行无需修改")
} else {
fmt.Printf("校验和不匹配,原校验和:%d,应该改为:%d\n", checksum, newChecksum)
fmt.Println("正确的行:")
fmt.Printf("%s%02X\n", line[:len(line)-2], newChecksum)
}
fmt.Println("按任意键退出...")
fmt.Scanln()
}
func reduce(nums []int) int {
return reduceHelper(nums, 0)
}
func reduceHelper(nums []int, acc int) int {
if len(nums) == 0 {
return acc % 256
}
return reduceHelper(nums[1:], (acc+nums[0])%256)
}
With this modification, the program will wait for the user to press any key before exiting.
[email protected]
fmt.Scanln()只能算是按回车键退出,不算是任意键退出
You are correct, waits for the user to input a complete line, which usually requires pressing the Enter key. If you want to wait for the user to press any key, you can use a platform-specific package like to read a single key without waiting for Enter to be pressed. Here's an example that uses this package to wait for the user to press any key:fmt.Scanln()github.com/eiannone/keyboard
go
Copy code
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/eiannone/keyboard"
)
func main() {
fmt.Println("请输入要计算校验和的行:")
reader := bufio.NewReader(os.Stdin)
line, := reader.ReadString('\n')
line = strings.TrimSpace(line)
checksum, := strconv.ParseInt(line[len(line)-2:], 16, 64)
pattern := regexp.MustCompile(`.{2}`)
parts := pattern.FindAllString(line[1:len(line)-2], -1)
var nums []int
for _, p := range parts {
num, _ := strconv.ParseInt(p, 16, 64)
nums = append(nums, int(num))
}
newChecksum := 256 - reduce(nums)
if checksum == int64(newChecksum) {
fmt.Println("校验和匹配,该行无需修改")
} else {
fmt.Printf("校验和不匹配,原校验和:%d,应该改为:%d\n", checksum, newChecksum)
fmt.Println("正确的行:")
fmt.Printf("%s%02X\n", line[:len(line)-2], newChecksum)
}
fmt.Println("按任意键退出...")
keyboard.GetSingleKey()
}
func reduce(nums []int) int {
return reduceHelper(nums, 0)
}
func reduceHelper(nums []int, acc int) int {
if len(nums) == 0 {
return acc % 256
}
return reduceHelper(nums[1:], (acc+nums[0])%256)
}
In this example, we use the function from the package to wait for the user to press any key before exiting. Note that this package may not work on all platforms, so you should check the documentation and test the program on the platforms you plan to use it on.GetSingleKey()github.com/eiannone/keyboard
一加8Pro