Skip to content

Instantly share code, notes, and snippets.

@flydo
Forked from zhang0098/gbkbig5.go
Created May 21, 2019 06:28
Show Gist options
  • Save flydo/44a4af97a5114ac39c93a781bd39a72d to your computer and use it in GitHub Desktop.
Save flydo/44a4af97a5114ac39c93a781bd39a72d 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