在 Go 中将 unicode 代码点转换为文字字符 [英] Convert unicode code point to literal character in Go

查看:59
本文介绍了在 Go 中将 unicode 代码点转换为文字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的文本文件.

Let's say I have a text file like this.

u0053
u0075
u006E

有什么办法可以把它转换成这个吗?

Is there a way I can convert that to this?

S
u
n

目前,我正在使用 ioutil.ReadFile("data.txt"),但是当我打印数据时,我得到的是 unicode 代码点而不是字符串文字.我意识到这是 ReadFile 的正确行为,这不是我想要的.

Currently, I'm using ioutil.ReadFile("data.txt"), but when I print the data, I get the unicode code points instead of the string literals. I realize this is the correct behavior for ReadFile, it's just not want I want.

我的目标是用它们的文字字符替换代码点.

I'm aiming for a substitution of the code points with their literal characters.

推荐答案

您可以使用 strconv.Unquote()strconv.UnquoteChar() 函数进行转换.

You can use the strconv.Unquote() and strconv.UnquoteChar() functions to do the conversion.

您应该注意的一件事是 strconv.Unquote() 只能取消引号中的字符串(例如以引号开头和结尾)"或反引号字符 `),所以我们必须手动附加它.

One thing you should be aware of is that strconv.Unquote() can only unquote strings that are in quotes (e.g. start and end with a quote char " or a back quote char `), so we have to manually append that.

看这个例子:

lines := []string{
    `u0053`,
    `u0075`,
    `u006E`,
}
fmt.Println(lines)

for i, v := range lines {
    var err error
    lines[i], err = strconv.Unquote(`"` + v + `"`)
    if err != nil {
        fmt.Println(err)
    }
}
fmt.Println(lines)

fmt.Println(strconv.Unquote(`"Gou0070x68x65x72"`))

输出(在 Go Playground 上试试):

Output (try it on the Go Playground):

[u0053 u0075 u006E]
[S u n]
Gopher <nil>

如果您要取消引用的字符串包含单个 rune 的转义序列(或者您只想取消引用第一个 rune),您可以使用 strconv.UnquoteChar().这是它的样子(注意:在这种情况下不需要引用输入,就像 strconv.Unquote() 需要它一样):

If the strings you want to unquote contain the escape sequence of a single rune (or you just want to unquote the first rune), you may use strconv.UnquoteChar(). This is how it looks like (note: no quoting of the input is needed in this case, like it was needed for strconv.Unquote()):

runes := []string{
    `u0053`,
    `u0075`,
    `u006E`,
}
fmt.Println(runes)

for _, v := range runes {
    var err error
    value, _, _, err := strconv.UnquoteChar(v, 0)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("%c
", value)
}

这将输出(在 Go Playground 上尝试):

This will output (try it on the Go Playground):

[u0053 u0075 u006E]
S
u
n

这篇关于在 Go 中将 unicode 代码点转换为文字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆