golang unicode转换为字符串string

2016-05-27 19:01  1389人阅读  评论 (0)
Tags: 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
}

https://play.golang.org/p/iHvnQpakqi