golang中,怎么把为ascii码的中文,转换为对应的中文

Python016

golang中,怎么把为ascii码的中文,转换为对应的中文,第1张

package main

import (

"fmt"

"code.google.com/p/mahonia"

)

func main() {

s := "\xb6\xd4\xb6\xc0\xc1\xa2\xd1\xa7\xd4\xba\xbf\xc9\xb3\xd6\xd0\xf8\xb7\xa2\xd5\xb9\xce\xca\xcc\xe2"

enc := mahonia.NewDecoder("gbk")

fmt.Println(enc.ConvertString(s))

}

代码仅供参考,请根据需要自行修改。

实现代码如下:a = 'abce'# print type(a)b = a.decode("ascii")# print type(b)c = a.decode("ascii").encode("utf-8")# print type(c)在python中进行编码转换都是通过unicode作为中间值实现的。所以要先decode成unicode字符,然后再使用encode转换成utf-8编码的str。可以把注释取消了,看下转换过程中的类型。