如何在Go中从编码转换为UTF-8? [英] How to convert from an encoding to UTF-8 in Go?

查看:314
本文介绍了如何在Go中从编码转换为UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我需要将文本从编码(例如Windows-1256阿拉伯语)转换为UTF-8.

I'm working on a project where I need to convert text from an encoding (for example Windows-1256 Arabic) to UTF-8.

我该如何在Go中执行此操作?

How do I do this in Go?

推荐答案

您可以使用编码包,其中包括通过包 golang.org/x/text/encoding/charmap 对Windows-1256的支持(在下面的示例中,导入此包并使用 charmap.Windows1256 而不是 japanese.ShiftJIS ).

You can use the encoding package, which includes support for Windows-1256 via the package golang.org/x/text/encoding/charmap (in the example below, import this package and use charmap.Windows1256 instead of japanese.ShiftJIS).

这是一个简短的示例,该示例将日语UTF-8字符串编码为ShiftJIS编码,然后将ShiftJIS字符串解码回UTF-8.不幸的是,它在操场上不起作用,因为操场上没有"x"包装.

Here's a short example which encodes a japanese UTF-8 string to ShiftJIS encoding and then decodes the ShiftJIS string back to UTF-8. Unfortunately it doesn't work on the playground since the playground doesn't have the "x" packages.

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)
}

日语StackOverflow网站上有一个更完整的示例.文字为日语,但代码应不言自明: https://ja.stackoverflow.com/questions/6120

There's a more complete example on the Japanese StackOverflow site. The text is Japanese, but the code should be self-explanatory: https://ja.stackoverflow.com/questions/6120

这篇关于如何在Go中从编码转换为UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆