本文实例讲述了golang实现unicode转换为字符串string的方法。分享给大家供大家参考,具体如下:
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"strings"
)
func main() {
str := `\u5bb6\u65cf`
fmt.Println(u2s(str))
}
func u2s(form string) (to string, err error) {
bs, err := hex.DecodeString(strings.Replace(form, `\u`, ``, -1))
if err != nil {
return
}
for i, bl, br, r := 0, len(bs), bytes.NewReader(bs), uint16(0); i < bl; i += 2 {
binary.Read(br, binary.BigEndian, &r)
to += string(r)
}
return
}
希望本文所述对大家Go语言程序设计有所帮助。