douxunchen3498
2017-12-29 09:33
浏览 371
已采纳

Golang符文字符串或如何转换?

I have a string which contains following text.

\xD0\xA4\xD0\xB5\xD0\xB4\xD0\xBE\xD1\x80\xD0\xBE\xD0\xB2

It is not a literal. In string it's stored as separate characters like this ['\','x','D','0','\','x','A','4',...]

How to convert this string to normal characters?

图片转代码服务由CSDN问答提供 功能建议

我有一个包含以下文本的字符串。

 <代码> \ XD0 \ XA4 \ XD0 \ XB5 \ XD0 \ XB4 \ XD0 \ XBE \ XD1 \ X80 \ XD0 \ XBE \ XD0 \ XB2 
   
 
 

这不是文字。 它以字符串形式存储为单独的字符,例如 ['\','x','D','0','\','x','A','4',...] < / code>

如何将此字符串转换为普通字符?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongqun1656 2017-12-29 09:42
    最佳回答

    Go accepts hexadecimal rune literals.

    So you can use your input as a regular string:

    fmt.Println("\xD0\xA4\xD0\xB5\xD0\xB4\xD0\xBE\xD1\x80\xD0\xBE\xD0\xB2")
    Федоров
    

    Playground example.

    If you start with the actual string ["\" "x" "D" "0" ...], you'll need to convert the individual 4-byte sequences to characters. One dirty way is:

    s := `\xD0\xA4\xD0\xB5\xD0\xB4\xD0\xBE\xD1\x80\xD0\xBE\xD0\xB2`
    s2, _ := hex.DecodeString(strings.Replace(s, "\\x", "", -1))
    fmt.Printf("%s", s2)
    

    Playground example.

    Edited to answer edited question.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题