我正在一个项目中,我需要将文本从编码(例如Windows-1256阿拉伯语)转换为UTF-8。
我该如何在Go中执行此操作?
您可以使用编码包,该编码包通过该包包括对Windows-1256的支持golang.org/x/text/encoding/charmap
(在下面的示例中,导入该包并使用charmap.Windows1256
代替japanese.ShiftJIS
)。
这是一个简短的示例,该示例将日语UTF-8字符串编码为ShiftJIS编码,然后将ShiftJIS字符串解码回UTF-8。不幸的是,它在操场上不起作用,因为操场上没有“ x”包装。
package main
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
func main() {
// the string we want to transform
s := "今日は"
fmt.Println(s)
// --- Encoding: convert s from UTF-8 to ShiftJIS
// declare a bytes.Buffer b and an encoder which will write into this buffer
var b bytes.Buffer
wInUTF8 := transform.NewWriter(&b, japanese.ShiftJIS.NewEncoder())
// encode our string
wInUTF8.Write([]byte(s))
wInUTF8.Close()
// print the encoded bytes
fmt.Printf("%#v\n", b)
encS := b.String()
fmt.Println(encS)
// --- Decoding: convert encS from ShiftJIS to UTF8
// declare a decoder which reads from the string we have just encoded
rInUTF8 := transform.NewReader(strings.NewReader(encS), japanese.ShiftJIS.NewDecoder())
// decode our string
decBytes, _ := ioutil.ReadAll(rInUTF8)
decS := string(decBytes)
fmt.Println(decS)
}
Japanese StackOverflow网站上有一个更完整的示例。文字为日语,但代码应不言自明:https://ja.stackoverflow.com/questions/6120
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句