golang实现unicode码和中文之间的转换
1 package main
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 )
8
9 func main() {
10 sText := "中文"
11 textQuoted := strconv.QuoteToASCII(sText)
12 textUnquoted := textQuoted[1 : len(textQuoted)-1]
13 fmt.Println(textUnquoted)
14
15 sUnicodev := strings.Split(textUnquoted, "\\u")
16 var context string
17 for _, v := range sUnicodev {
18 if len(v) < 1 {
19 continue
20 }
21 temp, err := strconv.ParseInt(v, 16, 32)
22 if err != nil {
23 panic(err)
24 }
25 context += fmt.Sprintf("%c", temp)
26 }
27 fmt.Println(context)
28 }
运行结果:
1 \u4e2d\u6587
2 中文