Skip to content

Instantly share code, notes, and snippets.

@zhang0098
Last active September 14, 2022 13:32
Show Gist options
  • Save zhang0098/c691e1da5bbdc7f41ca5 to your computer and use it in GitHub Desktop.
Save zhang0098/c691e1da5bbdc7f41ca5 to your computer and use it in GitHub Desktop.
Golang GBK Big5 from/to UTF-8 转换
import (
"bytes"
"io/ioutil"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
//convert GBK to UTF-8
func Decodegbk(s []byte) ([]byte, error) {
I := bytes.NewReader(s)
O := transform.NewReader(I, simplifiedchinese.GBK.NewDecoder())
d, e := ioutil.ReadAll(O)
if e != nil {
return nil, e
}
return d, nil
}
//convert BIG5 to UTF-8
func Decodebig5(s []byte) ([]byte, error) {
I := bytes.NewReader(s)
O := transform.NewReader(I, traditionalchinese.Big5.NewDecoder())
d, e := ioutil.ReadAll(O)
if e != nil {
return nil, e
}
return d, nil
}
//convert UTF-8 to BIG5
func Encodebig5(s []byte) ([]byte, error) {
I := bytes.NewReader(s)
O := transform.NewReader(I, traditionalchinese.Big5.NewEncoder())
d, e := ioutil.ReadAll(O)
if e != nil {
return nil, e
}
return d, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment