1 Star 1 Fork 0

minph / siphan

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
hex.go 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
Roger 提交于 2022-04-05 20:06 . 实现 console part
package diycode
import (
"fmt"
"strconv"
)
// 16进制字符串
type HexKey struct {
List []string
OrderSlice CodeSlice
}
// 通过指定的有序的字符项数组
// 令乱序的字符项数组生成16进制字符串
func (c CodeSlice) GenHexKeyFromOrderSlice(orderSlice *CodeSlice) (*HexKey, error) {
res := HexKey{
List: []string{},
OrderSlice: *orderSlice,
}
for _, item := range c {
target, err := orderSlice.FindChar(item.Char)
if err != nil {
return nil, err
}
key := fmt.Sprintf("%02X", target.Index)
res.List = append(res.List, key)
}
return &res, nil
}
// 通过指定的有序的字符项数组
//
// 默认指定为 diycode.CodeStr
//
// 令乱序的字符项数组生成16进制字符串
func (c CodeSlice) GenHexKey() (*HexKey, error) {
codeSlice := GetCodeSliceFromStr(CodeStr)
return c.GenHexKeyFromOrderSlice(&codeSlice)
}
func (h *HexKey) PrintStringText() string {
var str string
for i, key := range h.List {
var adder string
if i == 0 {
adder = key
} else {
adder = " " + key
}
str += adder
}
return str
}
func (h *HexKey) ParseToCodeSlice() (CodeSlice, error) {
res := make(CodeSlice, len(h.List))
for i, item := range h.List {
num, err := strconv.ParseInt(item, 16, 32)
if err != nil {
return nil, err
}
target, err := h.OrderSlice.FindIndex(int(num))
if err != nil {
return nil, err
}
res[i] = *target
}
return res, nil
}
// 反解原始字符串
func (h *HexKey) ParseOriginText() (string, error) {
var res string
for _, item := range h.List {
num, err := strconv.ParseInt(item, 16, 32)
if err != nil {
return "", err
}
target, err := h.OrderSlice.FindIndex(int(num))
if err != nil {
return "", err
}
res += target.Char
}
return res, nil
}
1
https://gitee.com/minph/siphan.git
git@gitee.com:minph/siphan.git
minph
siphan
siphan
43e49cbf781c

搜索帮助