1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 03:02:28 +08:00

feat: add Utf8ToGbk and GbkToUtf8

This commit is contained in:
dudaodong
2023-06-08 15:50:44 +08:00
parent 7311f84772
commit 83832daeb1
3 changed files with 70 additions and 0 deletions

View File

@@ -11,12 +11,15 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"math"
"reflect"
"strconv"
"strings"
"github.com/duke-git/lancet/v2/structs"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
// ToBool convert string to boolean.
@@ -375,3 +378,19 @@ func ToInterface(v reflect.Value) (value interface{}, ok bool) {
return nil, false
}
}
// Utf8ToGbk convert utf8 encoding to GBK encoding.
// Play: todo
func Utf8ToGbk(bs []byte) ([]byte, error) {
r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewEncoder())
b, err := io.ReadAll(r)
return b, err
}
// GbkToUtf8 convert GBK encoding to utf8 encoding.
// Play: todo
func GbkToUtf8(bs []byte) ([]byte, error) {
r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewDecoder())
b, err := io.ReadAll(r)
return b, err
}