golang中Gbk与Utf8编码格式相互转换

package utils

import (

"bytes"

"golang.org/x/text/encoding/simplifiedchinese"

"golang.org/x/text/transform"

"io/ioutil"

)

//gbk转utf8

func GbkToUtf8(s []byte) ([]byte, error) {

reader :=transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())

d, e :=ioutil.ReadAll(reader)

if e !=nil {

return nil, e

}

return d, nil

}

//utf8转gbk

func Utf8ToGbk(s []byte) ([]byte, error) {

reader :=transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())

d, e :=ioutil.ReadAll(reader)

if e !=nil {

return nil, e

}

return d, nil

}

你可能感兴趣的:(golang中Gbk与Utf8编码格式相互转换)