Golang GBK与UTF-8互转

前端之家收集整理的这篇文章主要介绍了Golang GBK与UTF-8互转前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

猜你在找的Go相关文章