diff --git a/cmd/crc16.go b/cmd/crc16.go index 50a6a52..342ec81 100644 --- a/cmd/crc16.go +++ b/cmd/crc16.go @@ -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) } diff --git a/cmd/crc32.go b/cmd/crc32.go index e0b8818..314fee7 100644 --- a/cmd/crc32.go +++ b/cmd/crc32.go @@ -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) } diff --git a/cmd/crc64.go b/cmd/crc64.go index 648a4aa..cb9003f 100644 --- a/cmd/crc64.go +++ b/cmd/crc64.go @@ -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) } diff --git a/types/endian.go b/types/endian.go new file mode 100644 index 0000000..a6eaf53 --- /dev/null +++ b/types/endian.go @@ -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" +}