将十六进制转换为字母。

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

Convert hex to alphabet

问题

如何在Go中从十六进制值获取字母值?

  1. package main
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. )
  6. func main() {
  7. a := []byte{0x61}
  8. c := hex.Dump(a)
  9. fmt.Println(c, a)
  10. }

你可以使用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?

  1. package main
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. )
  6. func main() {
  7. a := []byte{0x61}
  8. c := hex.Dump(a)
  9. fmt.Println(c,a)
  10. }

http://play.golang.org/p/7iAs2kKw5v

答案1

得分: 3

你可以使用fmt.Printf()函数进行格式化输出(示例):

  1. func main() {
  2. a := []byte{0x61}
  3. c := hex.Dump(a)
  4. fmt.Printf("'%+v' -- '%s'\n", c, a)
  5. }

输出结果为:

  1. '00000000 61 |a|
  2. ' -- 'a'

使用%s格式就足够将0x61转换为'a'

英文:

You could use a fmt.Printf() format (example):

  1. func main() {
  2. a := []byte{0x61}
  3. c := hex.Dump(a)
  4. fmt.Printf("'%+v' -- '%s'\n", c, a)
  5. }

Output:

  1. '00000000 61 |a|
  2. ' -- 'a'

The %s format is enough to convert the 0x61 in 'a'.

答案2

得分: 1

你的问题有点误导性。

根据你的问题,你实际上想要将一个 byte 值或者一个 []byte(字节切片)转换为一个 string 或者字符(在 Go 中更接近于 rune)。

因此,我将使用以下变量来区分单个的 byte 值和 []byte

  1. b := byte(0x61)
  2. bs := []byte{b}

要将其转换为 string,你可以简单地使用类型转换,这是最干净和最简单的方法:

  1. s := string(bs)

如果你想要它作为一个“字符”,你可以将其转换为 rune

  1. r := rune(b)

另一种解决方案是使用 fmt.Printf(),如 VonC 的答案 中所提到的,并使用 %s 占位符,它表示:

  • %s:字符串或切片的未解释字节
  • %c:对应的 Unicode 代码点所表示的字符
  • %q:使用 Go 语法安全转义的单引号字符字面量

%q 可以接受 byte[]byterune

看一下下面的示例来演示这些(在 Go Playground 上尝试一下):

  1. b := byte(0x61)
  2. bs := []byte{b}
  3. fmt.Printf("%s\n", bs)
  4. fmt.Printf("%c\n", b)
  5. fmt.Println(string(bs))
  6. fmt.Printf("%q\n", bs)
  7. fmt.Printf("%q\n", b)
  8. fmt.Printf("%q\n", rune(b))

输出结果:

  1. a
  2. a
  3. a
  4. "a"
  5. 'a'
  6. 'a'

如果你需要将结果作为 string,你可以使用 satran 的答案 中提到的 fmt.Sprintf() 变体,像这样:

  1. 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:

  1. b := byte(0x61)
  2. bs := []byte{b}

To convert it to a string, you can simply use a conversion which is the cleanest and most simple:

  1. s := string(bs)

If you want it as a "character", you can convert it to a rune:

  1. r := rune(b)

Another solution is using fmt.Printf() as mentioned in VonC's answer and using the %s verb which is:

  1. %s the uninterpreted bytes of the string or slice

You might want to take a look at these alternatives:

  1. %c the character represented by the corresponding Unicode code point
  2. %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):

  1. b := byte(0x61)
  2. bs := []byte{b}
  3. fmt.Printf("%s\n", bs)
  4. fmt.Printf("%c\n", b)
  5. fmt.Println(string(bs))
  6. fmt.Printf("%q\n", bs)
  7. fmt.Printf("%q\n", b)
  8. fmt.Printf("%q\n", rune(b))

Output:

  1. a
  2. a
  3. a
  4. "a"
  5. 'a'
  6. 'a'

If you need the result as a string, you can use the fmt.Sprintf() variant mentioned in satran's answer like this:

  1. s := fmt.Sprintf("%s", bs)

But it's easier to just use the string conversion (string(bs)).

答案3

得分: 0

如果你只想要字符串,你可以使用fmt.Sprintf

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. a := []byte{0x61}
  7. c := fmt.Sprintf("%s", a)
  8. fmt.Println(c)
  9. }

如果你只想要字符串,你可以使用fmt.Sprintf函数。在上面的代码中,我们定义了一个字节切片a,其中包含一个字节0x61。然后,我们使用fmt.Sprintf将字节切片转换为字符串,并将结果存储在变量c中。最后,我们使用fmt.Println打印出字符串c的值。

英文:

If you just want the string you can you fmt.Sprintf.

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. a := []byte{0x61}
  7. c := fmt.Sprintf("%s", a)
  8. fmt.Println(c)
  9. }

huangapple
  • 本文由 发表于 2015年3月5日 22:21:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/28880032.html
匿名

发表评论

匿名网友

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

确定