golang implements the method of converting unicode to the string string
- 2020-06-01 10:00:08
- OfStack
The example in this article describes how golang converts unicode to the string string. I will share it with you for your reference as follows:
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
}
I hope this article has been helpful to you in the programming of Go language.