packagemain
import(
"code.google.com/p/mahonia"
"fmt"
)
funcmain(){
//"你好,世界!"的GBK编码
testBytes:=[]byte{0xC4,0xE3,0xBA,0xC3,0xA3,0xAC,0xCA,0xC0,0xBD,0xE7,0xA1}
vartestStrstring
utfStr:="你好,世界!"
vardecmahonia.Decoder
varencmahonia.Encoder
testStr=string(testBytes)
dec=mahonia.NewDecoder("gbk")
ifret,ok:=dec.ConvertStringOK(testStr);ok{
fmt.Println("GBKtoUTF-8:",ret,"bytes:",testBytes)
}
enc=mahonia.NewEncoder("gbk")
ifret,ok:=enc.ConvertStringOK(utfStr);ok{
fmt.Println("UTF-8toGBK:",[]byte(ret))
}
return
}
原文链接:https://www.f2er.com/go/190576.html