살인!
golang
golang - golang iconv 문자 인코딩 변환
이 름 : 바다아이
|
조회수 : 12062
짧은 주소 : https://www.bada-ie.com/su/?841591843817
패키지 가져와야죠..
go get golang.org/x/ text
아래는 예제 입니다.
import (
"bytes"
"errors"
"io/ioutil"
"strings"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/korean"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/transform"
)
func ToUTF8(from string, s []byte ) ([]byte , error) {
var reader *transform.Reader
switch strings.ToLower(from ) {
case "gbk" , "cp936" , "windows-936" :
reader = transform.NewReader (bytes.NewReader (s), simplifiedchinese.GBK.NewDecoder())
case "gb18030" :
reader = transform.NewReader (bytes.NewReader (s), simplifiedchinese.GB18030.NewDecoder())
case "gb2312" :
reader = transform.NewReader (bytes.NewReader (s), simplifiedchinese.HZGB2312.NewDecoder())
case "big5" , "big-5" , "cp950" :
reader = transform.NewReader (bytes.NewReader (s), traditionalchinese.Big5.NewDecoder())
case "euc-kr" , "euckr" , "cp949" :
reader = transform.NewReader (bytes.NewReader (s), korean.EUCKR.NewDecoder())
case "euc-jp" , "eucjp" :
reader = transform.NewReader (bytes.NewReader (s), japanese.EUCJP.NewDecoder())
case "shift-jis" :
reader = transform.NewReader (bytes.NewReader (s), japanese.ShiftJIS.NewDecoder())
case "iso-2022-jp" , "cp932" , "windows-31j" :
reader = transform.NewReader (bytes.NewReader (s), japanese.ISO2022JP.NewDecoder())
default :
return s, errors.New ("Unsupported encoding " + from )
}
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}
func FromUTF8(to string, s []byte ) ([]byte , error) {
var reader *transform.Reader
switch strings.ToLower(to) {
case "gbk" , "cp936" , "windows-936" :
reader = transform.NewReader (bytes.NewReader (s), simplifiedchinese.GBK.NewEncoder())
case "gb18030" :
reader = transform.NewReader (bytes.NewReader (s), simplifiedchinese.GB18030.NewEncoder())
case "gb2312" :
reader = transform.NewReader (bytes.NewReader (s), simplifiedchinese.HZGB2312.NewEncoder())
case "big5" , "big-5" , "cp950" :
reader = transform.NewReader (bytes.NewReader (s), traditionalchinese.Big5.NewEncoder())
case "euc-kr" , "euckr" , "cp949" :
reader = transform.NewReader (bytes.NewReader (s), korean.EUCKR.NewEncoder())
case "euc-jp" , "eucjp" :
reader = transform.NewReader (bytes.NewReader (s), japanese.EUCJP.NewEncoder())
case "shift-jis" :
reader = transform.NewReader (bytes.NewReader (s), japanese.ShiftJIS.NewEncoder())
case "iso-2022-jp" , "cp932" , "windows-31j" :
reader = transform.NewReader (bytes.NewReader (s), japanese.ISO2022JP.NewEncoder())
default :
return s, errors.New ("Unsupported encoding " + to)
}
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}
출처 : https:
|
|