commit
e5df799c8c
@ -0,0 +1,2 @@
|
|||||||
|
.idea
|
||||||
|
go.sum
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package str
|
||||||
|
|
||||||
|
import "encoding/hex"
|
||||||
|
|
||||||
|
type Str struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// struing to hex
|
||||||
|
func HexEncode(str string) string {
|
||||||
|
byteStr := []byte(str)
|
||||||
|
return hex.EncodeToString(byteStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// hex to string
|
||||||
|
func HexDecode(str string) (string, error) {
|
||||||
|
hexStr, err := hex.DecodeString(str)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(hexStr), nil
|
||||||
|
}
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
package str
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHexEncode(t *testing.T) {
|
||||||
|
fmt.Println(HexEncode("hello world!"))
|
||||||
|
//68656c6c6f20776f726c6421
|
||||||
|
}
|
||||||
|
func TestHexDecode(t *testing.T) {
|
||||||
|
fmt.Println(HexDecode("68656c6c6f20776f726c6421"))
|
||||||
|
//hello world! <nil>
|
||||||
|
}
|
||||||
Loading…
Reference in new issue