如何在Golang中将[] uint8转换为字符串

我在Go中观察程序的日志,并且在输出中看到以下内容:

[]uint8=[18 32 96 38 15 212 226 233 58 14 183 212 80 4 175 97 28 235 76 154 244 210 200 18 131 241 31 86 179 109 228 190 78 72])

Now I would like to be able to convert this to a form where I can see what it means. This should be string content represented as []uint8.

But when I call the string method on that value as suggested in this link here I get a gibberish output.

Go Playground link here

我可能在做错什么,以及如何进行这种转换?

评论
jsed
jsed

The conversion s := string(stuff) is fine, but usually you'd expect stuff to contain actual printable character sequences. In your case, it doesn't (mostly).

Make sure to read Strings, bytes, runes and characters in Go in detail.

修改示例以打印具有ASCII字母映射的字节值:

package main

import (
    "fmt"
)

func main() {
    stuff := []byte{97, 98, 99, 100}
    s := string(stuff)
    fmt.Println(s)
}

Prints "abcd".

点赞
评论