英文:
Convert hex to alphabet
问题
如何在Go中从十六进制值获取字母值?
package main
import (
"encoding/hex"
"fmt"
)
func main() {
a := []byte{0x61}
c := hex.Dump(a)
fmt.Println(c, a)
}
你可以使用encoding/hex包中的Dump函数将字节切片转换为十六进制字符串表示形式。在上面的示例中,我们将字节切片a中的十六进制值0x61转换为字符串,并使用fmt.Println打印出来。
你可以在这里运行这段代码:http://play.golang.org/p/7iAs2kKw5v
英文:
How do I obtain the alphabet value from the hex value in Go?
package main
import (
"encoding/hex"
"fmt"
)
func main() {
a := []byte{0x61}
c := hex.Dump(a)
fmt.Println(c,a)
}
答案1
得分: 3
你可以使用fmt.Printf()函数进行格式化输出(示例):
func main() {
a := []byte{0x61}
c := hex.Dump(a)
fmt.Printf("'%+v' -- '%s'\n", c, a)
}
输出结果为:
'00000000 61 |a|
' -- 'a'
使用%s格式就足够将0x61转换为'a'。
英文:
You could use a fmt.Printf() format (example):
func main() {
a := []byte{0x61}
c := hex.Dump(a)
fmt.Printf("'%+v' -- '%s'\n", c, a)
}
Output:
'00000000 61 |a|
' -- 'a'
The %s format is enough to convert the 0x61 in 'a'.
答案2
得分: 1
你的问题有点误导性。
根据你的问题,你实际上想要将一个 byte 值或者一个 []byte(字节切片)转换为一个 string 或者字符(在 Go 中更接近于 rune)。
因此,我将使用以下变量来区分单个的 byte 值和 []byte:
b := byte(0x61)
bs := []byte{b}
要将其转换为 string,你可以简单地使用类型转换,这是最干净和最简单的方法:
s := string(bs)
如果你想要它作为一个“字符”,你可以将其转换为 rune:
r := rune(b)
另一种解决方案是使用 fmt.Printf(),如 VonC 的答案 中所提到的,并使用 %s 占位符,它表示:
%s:字符串或切片的未解释字节%c:对应的 Unicode 代码点所表示的字符%q:使用 Go 语法安全转义的单引号字符字面量
%q 可以接受 byte、[]byte 和 rune。
看一下下面的示例来演示这些(在 Go Playground 上尝试一下):
b := byte(0x61)
bs := []byte{b}
fmt.Printf("%s\n", bs)
fmt.Printf("%c\n", b)
fmt.Println(string(bs))
fmt.Printf("%q\n", bs)
fmt.Printf("%q\n", b)
fmt.Printf("%q\n", rune(b))
输出结果:
a
a
a
"a"
'a'
'a'
如果你需要将结果作为 string,你可以使用 satran 的答案 中提到的 fmt.Sprintf() 变体,像这样:
s := fmt.Sprintf("%s", bs)
但是更简单的方法是直接使用类型转换 string(bs)。
英文:
Your question is a little misleading.
Based on your question what you really want is convert a byte value or a []byte (byte slice) to a string or character (which is more or less a rune in Go).
Henceforth I will separate the single byte value from the []byte using these variables:
b := byte(0x61)
bs := []byte{b}
To convert it to a string, you can simply use a conversion which is the cleanest and most simple:
s := string(bs)
If you want it as a "character", you can convert it to a rune:
r := rune(b)
Another solution is using fmt.Printf() as mentioned in VonC's answer and using the %s verb which is:
%s the uninterpreted bytes of the string or slice
You might want to take a look at these alternatives:
%c the character represented by the corresponding Unicode code point
%q a single-quoted character literal safely escaped with Go syntax.
%q accepts both a byte, []byte and rune.
See this litte example to demonstrate these (try it on the Go Playground):
b := byte(0x61)
bs := []byte{b}
fmt.Printf("%s\n", bs)
fmt.Printf("%c\n", b)
fmt.Println(string(bs))
fmt.Printf("%q\n", bs)
fmt.Printf("%q\n", b)
fmt.Printf("%q\n", rune(b))
Output:
a
a
a
"a"
'a'
'a'
If you need the result as a string, you can use the fmt.Sprintf() variant mentioned in satran's answer like this:
s := fmt.Sprintf("%s", bs)
But it's easier to just use the string conversion (string(bs)).
答案3
得分: 0
如果你只想要字符串,你可以使用fmt.Sprintf。
package main
import (
"fmt"
)
func main() {
a := []byte{0x61}
c := fmt.Sprintf("%s", a)
fmt.Println(c)
}
如果你只想要字符串,你可以使用fmt.Sprintf函数。在上面的代码中,我们定义了一个字节切片a,其中包含一个字节0x61。然后,我们使用fmt.Sprintf将字节切片转换为字符串,并将结果存储在变量c中。最后,我们使用fmt.Println打印出字符串c的值。
英文:
If you just want the string you can you fmt.Sprintf.
package main
import (
"fmt"
)
func main() {
a := []byte{0x61}
c := fmt.Sprintf("%s", a)
fmt.Println(c)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论