2019-04-10 18:21


  • utf-8
  • unicode

I am running an executable from Go via os.Exec, which gives me the following output: (\\xe2\\x96\\xb2). The output contains a UTF-8 byte string, which I want to convert to the corresponding Unicode codepoint (U+25B2). What I am expecting to see, or trying to convert to is: "(▲)". I have looked at this entry in the Go Blog (https://blog.golang.org/strings), but it starts out with an Interpreted string literal, whereas the command output seems to be a Raw string literal. I have tried strconv.Quote and strconv.Unquote, which does not achieve what I'm looking for.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douyong4623 douyong4623 2年前

    You can use the strconv package to parse the string literal containing the escape sequences.

    The quick and dirty way is to simply add the missing quotes and interpret it as a quoted string using strconv.Unquote

    s := `\xe2\x96\xb2`
    s, err := strconv.Unquote(`"` + s + `"`)

    You can also directly parse the string one character at a time (which is what Unquote does internally), using strconv.UnquoteChar

    s := `\xe2\x96\xb2`
    buf := make([]byte, 0, 3*len(s)/2)
    for len(s) > 0 {
        c, _, ss, err := strconv.UnquoteChar(s, 0)
        if err != nil {
        s = ss
        buf = append(buf, byte(c))
    s = string(buf)


    点赞 评论 复制链接分享