如何在Go中将一个字符串转换为使用给定字符集编译的字节数组?

huangapple go评论85阅读模式
英文:

How to convert a string to a byte array which is compiled with a given charset in Go?

问题

在Go语言中,我们可以使用以下方法来实现类似的功能:\n\n\n
func GetBytes(s string) []byte {
return []byte(s)
}

这个方法将字符串转换为字节切片。希望对你有帮助。

英文:

In java, we can use the method of String : byte[] getBytes(Charset charset) .
This method Encodes a String into a sequence of bytes using the given charset, storing the result into a new byte array.

But how to do this in GO?
Is there any similar way in Go can do this?

Please let me know it.

答案1

得分: 7

标准的Go库只支持Unicode(UTF-8,UTF-16,UTF-32)和ASCII编码。 ASCII是UTF-8的子集。

go-charset包(从这里找到)支持转换为和从UTF-8,并且它还链接到GNU iconv库。

另请参见encoding/xml.Decoder中的CharsetReader字段。

英文:

The standard Go library only supports Unicode (UTF-8, UTF-16, UTF-32) and ASCII encoding. ASCII is a subset of UTF-8.

The go-charset package (found from here) supports conversion to and from UTF-8 and it also links to the GNU iconv library.

See also field CharsetReader in encoding/xml.Decoder.

答案2

得分: 1

我相信这里有一个答案:https://stackoverflow.com/a/6933412/1315563

> 没有办法在不自己编写转换代码或使用第三方包的情况下完成。你可以尝试使用这个:http://code.google.com/p/go-charset

英文:

I believe here is an answer: https://stackoverflow.com/a/6933412/1315563

> There is no way to do it without writing the conversion yourself or
> using a third-party package. You could try using this:
> http://code.google.com/p/go-charset

huangapple
  • 本文由 发表于 2012年4月6日 13:56:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/10039701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定