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

feat: add Utf8ToGbk and GbkToUtf8

This commit is contained in:
dudaodong
2023-06-13 14:49:40 +08:00
parent 5a8ff17b52
commit d20f8783b2
4 changed files with 180 additions and 0 deletions

View File

@@ -11,11 +11,15 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"math"
"reflect"
"regexp"
"strconv"
"strings"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
// ToBool convert string to a boolean
@@ -374,3 +378,17 @@ func ToInterface(v reflect.Value) (value interface{}, ok bool) {
return nil, false
}
}
// Utf8ToGbk convert utf8 encoding data to GBK encoding data.
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 data to utf8 encoding data.
func GbkToUtf8(bs []byte) ([]byte, error) {
r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewDecoder())
b, err := io.ReadAll(r)
return b, err
}