Last active
September 14, 2022 13:32
-
-
Save zhang0098/c691e1da5bbdc7f41ca5 to your computer and use it in GitHub Desktop.
Golang GBK Big5 from/to UTF-8 转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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