donglengyuan6826
2012-04-06 05:56
浏览 115
已采纳

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

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.

图片转代码服务由CSDN问答提供 功能建议

在Java中,我们可以使用String方法:byte [] getBytes(Charset charset)。 此方法编码 使用给定的字符集将String转换为字节序列,然后将结果存储到新的字节数组中。

但是如何在GO中执行此操作? Go中是否有任何类似的方法可以做到 这个?

请让我知道。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dougaoxian8922 2012-04-06 07:46
    已采纳

    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.

    点赞 打赏 评论
  • douxi1968 2012-04-06 07:47

    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

    点赞 打赏 评论

相关推荐 更多相似问题