mirror of
https://github.com/fumiama/go-base16384.git
synced 2026-06-12 14:10:32 +08:00
new api
This commit is contained in:
32
conv.go
32
conv.go
@@ -6,12 +6,36 @@ import (
|
||||
|
||||
var format = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
|
||||
|
||||
// UTF16be2utf8 to display the result as string
|
||||
func UTF16be2utf8(b []byte) ([]byte, error) {
|
||||
// UTF16BE2UTF8 to display the result as string
|
||||
func UTF16BE2UTF8(b []byte) ([]byte, error) {
|
||||
return format.NewDecoder().Bytes(b)
|
||||
}
|
||||
|
||||
// UTF82utf16be to decode from string
|
||||
func UTF82utf16be(b []byte) ([]byte, error) {
|
||||
// UTF82UTF16BE to decode from string
|
||||
func UTF82UTF16BE(b []byte) ([]byte, error) {
|
||||
return format.NewEncoder().Bytes(b)
|
||||
}
|
||||
|
||||
func EncodeFromString(s string) []byte {
|
||||
return Encode(StringToBytes(s))
|
||||
}
|
||||
|
||||
func EncodeString(s string) string {
|
||||
out, err := UTF16BE2UTF8(Encode(StringToBytes(s)))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return BytesToString(out)
|
||||
}
|
||||
|
||||
func DecodeToString(d []byte) string {
|
||||
return BytesToString(Decode(d))
|
||||
}
|
||||
|
||||
func DecodeString(s string) string {
|
||||
d, err := UTF82UTF16BE(StringToBytes(s))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return BytesToString(Decode(d))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user