英文:
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 < 128 {
return string(r)
} else {
return "\\u" + 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("%U", '啊')
fmt.Println("\\u" + 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 "fmt"
func main() {
r := rune('啊')
u := fmt.Sprintf("%U", r)
fmt.Println(string(r), u)
}
Output:
啊 U+554A
答案5
得分: 1
fmt.Printf("\u554a")
英文:
fmt.Printf("\\u%X", '啊')
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 "fmt"
func main() {
fmt.Printf("%+q", '啊')
}
答案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 < 128 {
return string(r)
} else {
return fmt.Sprintf("\\u%04x", r)
}
}
答案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 (
"fmt"
)
func main() {
str := fmt.Sprintf("%s", []byte{0x80})
fmt.Println(str)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论