enhance(crc):CRC系列校验算法增加使用大端序或者小端序输出的功能。
This commit is contained in:
parent
8719a0e689
commit
190dd36490
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
var (
|
||||
crc16Algorithm types.CRC16Algorithm = types.CRC16_IBM
|
||||
crc16Endian types.Endian = types.LittleEndian
|
||||
crc16File bool
|
||||
)
|
||||
|
||||
|
@ -36,6 +37,11 @@ func crc16Execute(cmd *cobra.Command, args []string) {
|
|||
fmt.Printf("计算CRC16校验值时发生错误:%s\n", err)
|
||||
return
|
||||
}
|
||||
if crc16Endian == types.LittleEndian {
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
}
|
||||
var builder strings.Builder
|
||||
for _, b := range result {
|
||||
builder.WriteString(fmt.Sprintf(" %02X", b))
|
||||
|
@ -45,6 +51,7 @@ func crc16Execute(cmd *cobra.Command, args []string) {
|
|||
|
||||
func init() {
|
||||
crc16Cmd.PersistentFlags().VarP(&crc16Algorithm, "algorithm", "a", "CRC16算法,可选值有:ccitt, ccitt-false, scsi, ibm, mbus,缺省值为ibm。")
|
||||
crc16Cmd.PersistentFlags().VarP(&crc16Endian, "endian", "e", "字节序,可选值有:big, little,缺省值为little。")
|
||||
crc16Cmd.PersistentFlags().BoolVarP(&crc16File, "file", "f", false, "从文件中读取内容")
|
||||
rootCmd.AddCommand(crc16Cmd)
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
var (
|
||||
crc32Algorithm types.CRC32Algorithm = types.CRC32_IEEE
|
||||
crc32Endian types.Endian = types.LittleEndian
|
||||
crc32File bool
|
||||
)
|
||||
|
||||
|
@ -36,6 +37,11 @@ func crc32Execute(cmd *cobra.Command, args []string) {
|
|||
fmt.Printf("计算CRC32校验值时发生错误:%s\n", err)
|
||||
return
|
||||
}
|
||||
if crc32Endian == types.LittleEndian {
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
}
|
||||
var builder strings.Builder
|
||||
for _, b := range result {
|
||||
builder.WriteString(fmt.Sprintf(" %02X", b))
|
||||
|
@ -45,6 +51,7 @@ func crc32Execute(cmd *cobra.Command, args []string) {
|
|||
|
||||
func init() {
|
||||
crc32Cmd.PersistentFlags().VarP(&crc32Algorithm, "algorithm", "a", "CRC32算法,可选值有:ieee, castagnoli, koopman,缺省值为ieee。")
|
||||
crc32Cmd.PersistentFlags().VarP(&crc32Endian, "endian", "e", "字节序,可选值有:big, little,缺省值为little。")
|
||||
crc32Cmd.PersistentFlags().BoolVarP(&crc32File, "file", "f", false, "从文件中读取内容")
|
||||
rootCmd.AddCommand(crc32Cmd)
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
var (
|
||||
crc64Algorithm types.CRC64Algorithm = types.CRC64_ISO
|
||||
crc64Endian types.Endian = types.LittleEndian
|
||||
crc64File bool
|
||||
)
|
||||
|
||||
|
@ -36,6 +37,11 @@ func crc64Execute(cmd *cobra.Command, args []string) {
|
|||
fmt.Printf("计算CRC64校验值时发生错误:%s\n", err)
|
||||
return
|
||||
}
|
||||
if crc64Endian == types.LittleEndian {
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
}
|
||||
var builder strings.Builder
|
||||
for _, b := range result {
|
||||
builder.WriteString(fmt.Sprintf(" %02X", b))
|
||||
|
@ -45,6 +51,7 @@ func crc64Execute(cmd *cobra.Command, args []string) {
|
|||
|
||||
func init() {
|
||||
crc64Cmd.PersistentFlags().VarP(&crc64Algorithm, "algorithm", "a", "CRC64算法,可选值有:iso, ecma,缺省值为iso。")
|
||||
crc64Cmd.PersistentFlags().VarP(&crc64Endian, "endian", "e", "字节序,可选值有:big, little,缺省值为little。")
|
||||
crc64Cmd.PersistentFlags().BoolVarP(&crc64File, "file", "f", false, "从文件中读取内容")
|
||||
rootCmd.AddCommand(crc64Cmd)
|
||||
}
|
||||
|
|
28
types/endian.go
Normal file
28
types/endian.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package types
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Endian string
|
||||
|
||||
const (
|
||||
BigEndian Endian = "big"
|
||||
LittleEndian Endian = "little"
|
||||
)
|
||||
|
||||
func (e *Endian) String() string {
|
||||
return string(*e)
|
||||
}
|
||||
|
||||
func (e *Endian) Set(s string) error {
|
||||
switch s {
|
||||
case "big", "little":
|
||||
*e = Endian(s)
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("不支持的字节序:%s", s)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Endian) Type() string {
|
||||
return "Endian"
|
||||
}
|
Loading…
Reference in New Issue
Block a user