golang实现unicode转换为字符串string的方法
时间:2021-07-27 08:18:09|栏目:Golang|点击: 次
本文实例讲述了golang实现unicode转换为字符串string的方法。分享给大家供大家参考,具体如下:
复制代码 代码如下:
package main
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
}
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语言程序设计有所帮助。
栏 目:Golang
下一篇:golang常用库之gorilla/mux-http路由库使用详解
本文标题:golang实现unicode转换为字符串string的方法
本文地址:http://www.codeinn.net/misctech/161708.html