如何在Golang中将rune转换为类似`\u554a`的Unicode风格字符串?

huangapple go评论85阅读模式
英文:

How to convert a rune to unicode-style-string like `\u554a` in Golang?

问题

如果你运行 fmt.Println("\u554a"),它会显示 '啊'。

但是如何从一个字符 '啊' 获取 Unicode 格式的字符串 \u554a

英文:

If you run fmt.Println("\u554a"), it shows '啊'.

But how to get unicode-style-string \u554a from a rune '啊' ?

答案1

得分: 18

\u554a

英文:
package main

import "fmt"
import "strconv"

func main() {
    quoted := strconv.QuoteRuneToASCII('啊') // quoted = "'\u554a'"
    unquoted := quoted[1:len(quoted)-1]      // unquoted = "\u554a"
    fmt.Println(unquoted)
}

This outputs:

<pre>
\u554a
</pre>

答案2

得分: 13

func RuneToAscii(r rune) string {
if r < 128 {
return string(r)
} else {
return "\u" + strconv.FormatInt(int64(r), 16)
}
}

英文:

IMHO, it should be better:

func RuneToAscii(r rune) string {
	if r &lt; 128 {
		return string(r)
	} else {
		return &quot;\\u&quot; + strconv.FormatInt(int64(r), 16)
	}
}

答案3

得分: 4

你可以使用fmt.Sprintf%U来获取十六进制值:

test = fmt.Sprintf("%U", '啊')
fmt.Println("\\u" + test[2:]) // 打印 \u554A
英文:

You can use fmt.Sprintf along with %U to get the hexadecimal value:

test = fmt.Sprintf(&quot;%U&quot;, &#39;啊&#39;)
fmt.Println(&quot;\\u&quot; + test[2:]) // Print \u554A

答案4

得分: 1

例如,

package main

import "fmt"

func main() {
    r := rune('啊')
    u := fmt.Sprintf("%U", r)
    fmt.Println(string(r), u)
}

输出:

啊 U+554A
英文:

For example,

package main

import &quot;fmt&quot;

func main() {
	r := rune(&#39;啊&#39;)
	u := fmt.Sprintf(&quot;%U&quot;, r)
	fmt.Println(string(r), u)
}

Output:

啊 U+554A

答案5

得分: 1

fmt.Printf("\u554a")

英文:
fmt.Printf(&quot;\\u%X&quot;, &#39;啊&#39;)

http://play.golang.org/p/Jh9ns8Qh15

(Upper or lowercase 'x' will control the case of the hex characters)

As hinted at by package fmt's documentation:

> %U Unicode format: U+1234; same as "U+%04X"

答案6

得分: 1

package main

import "fmt"

func main() {
fmt.Printf("%+q", '啊')
}

英文:
package main

import &quot;fmt&quot;

func main() {
    fmt.Printf(&quot;%+q&quot;, &#39;啊&#39;)
}

答案7

得分: 0

我想补充一下hardPass的答案。

在unicode的十六进制表示中,如果少于4个字符(例如ü),strconv.FormatInt将会得到\ufc,这会导致Go语言出现unicode语法错误。与Go能够理解的完整的\u00fc相反。

使用fmt.Sprintf和十六进制格式化,用零填充十六进制可以解决这个问题:

func RuneToAscii(r rune) string {
    if r < 128 {
        return string(r)
    } else {
        return fmt.Sprintf("\\u%04x", r)
    }
}

https://play.golang.org/p/80w29oeBec1

英文:

I'd like to add to the answer that hardPass has.

In the case where the hex representation of the unicode is less that 4 characters (ü for example) strconv.FormatInt will result in \ufc which will result in a unicode syntax error in Go. As opposed to the full \u00fc that Go understands.

Padding the hex with zeros using fmt.Sprintf with hex formatting will fix this:

func RuneToAscii(r rune) string {
	if r &lt; 128 {
		return string(r)
	} else {
		return fmt.Sprintf(&quot;\\u%04x&quot;, r)
	}
}

https://play.golang.org/p/80w29oeBec1

答案8

得分: 0

这将完成工作...

<!-- language: lang-or-tag-here -->

package main

import (
	"fmt"
)

func main() {
	str := fmt.Sprintf("%s", []byte{0x80})
	fmt.Println(str)
}
英文:

This would do the job..

<!-- language: lang-or-tag-here -->

package main

import (
	&quot;fmt&quot;
)

func main() {
	str := fmt.Sprintf(&quot;%s&quot;, []byte{0x80})
	fmt.Println(str)
}

huangapple
  • 本文由 发表于 2013年5月22日 11:02:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/16682797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定