feat(serialize):基本完成Hex和Base64的编解码。
This commit is contained in:
26
serialize/hex/hex.go
Normal file
26
serialize/hex/hex.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// 提供字节数组到十六进制字符串的快捷转换
|
||||
package hex
|
||||
|
||||
import "encoding/hex"
|
||||
|
||||
type HexSerializeError struct {
|
||||
source *error
|
||||
}
|
||||
|
||||
func (e *HexSerializeError) Error() string {
|
||||
return "无法将字节数组转换为十六进制字符串。"
|
||||
}
|
||||
|
||||
// 将给定的字节数组转换为十六进制字符串。
|
||||
func ToHex(b []byte) string {
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
// 将给定的十六进制字符串转换为字节数组。如果不能成功完成转换将返回错误和一个空字节数组。
|
||||
func FromHex(s string) ([]byte, error) {
|
||||
b, err := hex.DecodeString(s)
|
||||
if err != nil {
|
||||
return make([]byte, 0), &HexSerializeError{source: &err}
|
||||
}
|
||||
return b, nil
|
||||
}
|
Reference in New Issue
Block a user