英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论