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

佐手、 提交于 2019-12-12 16:37:57

问题


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:


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:


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



来源:https://stackoverflow.com/questions/10039701/how-to-convert-a-string-to-a-byte-array-which-is-compiled-with-a-given-charset-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!