2024-01-27 14:36:14 +08:00
|
|
|
package convutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BinaryConvert struct{}
|
|
|
|
|
2024-02-20 10:55:46 +08:00
|
|
|
func (BinaryConvert) Enable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-01-27 14:36:14 +08:00
|
|
|
func (BinaryConvert) Encode(str string) (string, bool) {
|
|
|
|
var result strings.Builder
|
|
|
|
total := len(str)
|
|
|
|
for i := 0; i < total; i += 8 {
|
|
|
|
b, _ := strconv.ParseInt(str[i:i+8], 2, 8)
|
|
|
|
result.WriteByte(byte(b))
|
|
|
|
}
|
|
|
|
return result.String(), true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (BinaryConvert) Decode(str string) (string, bool) {
|
|
|
|
var binary strings.Builder
|
2024-06-05 10:46:38 +08:00
|
|
|
for _, char := range []byte(str) {
|
2024-01-27 14:36:14 +08:00
|
|
|
binary.WriteString(fmt.Sprintf("%08b", int(char)))
|
|
|
|
}
|
|
|
|
return binary.String(), true
|
|
|
|
}
|