Files
blivedm-go/utils/strconv.go
2023-08-12 21:43:21 +08:00

30 lines
493 B
Go

package utils
import (
"encoding/base64"
"unsafe"
)
func StringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func B64Decode(s string) ([]byte, error) {
dst := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
n, err := base64.StdEncoding.Decode(dst, []byte(s))
if err != nil {
return dst, err
}
dst = dst[:n]
return dst, nil
}