master
sre 4 years ago
parent e5df799c8c
commit 9dc83b2157

@ -6,13 +6,13 @@ type Str struct {
}
// struing to hex
func HexEncode(str string) string {
func (s *Str) HexEncode(str string) string {
byteStr := []byte(str)
return hex.EncodeToString(byteStr)
}
// hex to string
func HexDecode(str string) (string, error) {
func (s *Str) HexDecode(str string) (string, error) {
hexStr, err := hex.DecodeString(str)
if err != nil {
panic(err)

@ -5,11 +5,13 @@ import (
"testing"
)
var StrTool Str
func TestHexEncode(t *testing.T) {
fmt.Println(HexEncode("hello world!"))
fmt.Println(StrTool.HexEncode("hello world!"))
//68656c6c6f20776f726c6421
}
func TestHexDecode(t *testing.T) {
fmt.Println(HexDecode("68656c6c6f20776f726c6421"))
fmt.Println(StrTool.HexDecode("68656c6c6f20776f726c6421"))
//hello world! <nil>
}

Loading…
Cancel
Save