字节数组的字符串值 – 再转回字节数组

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

string value of byte array - and back again to byte array

问题

我正在尝试对一些解码代码进行基准测试。我已经打印了编码的字节数组,所以我可以在测试文件中硬编码它,以分离出编码部分。

因此,我使用了fmt.Println()来打印一个字节数组:

buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
err := enc.Encode(<my struct>)
if err != nil {
    log.Fatal(err)
}
byteQuads := buf.Bytes()
buf.Reset()
fmt.Println(byteQuads) // << [77 255 129 3 1 1 7 82...]
// 或者
fmt.Println(string(byteQuads[:])) // << M��

问题:如何将这个硬编码的字节数组([]byte)再次读取为一个字节数组?

b := []byte(???) // [77 255 129 3 1 1 7 82...] 或者 M��
gob.NewDecoder(bytes.NewReader(b))

请注意,我只提供了代码的翻译部分,不包括其他内容。

英文:

I am trying to benchmark some Decoding code. I have printed the encoding byte array, so I can hardcode it in the test file, to separate out the Encode part.

Therefore I have fmt.Println() a byte array

buf := bytes.Buffer{}
enc := gob.NewEncoder(&amp;buf)
err := enc.Encode(&lt;my struct&gt;)
if err != nil {
	log.Fatal(err)
}
byteQuads := buf.Bytes()
buf.Reset()
fmt.Println(byteQuads) // &lt;&lt; [77 255 129 3 1 1 7 82...]
// or
fmt.Println(string(byteQuads[:])) // &lt;&lt; M��...

Question: How can read this hardcode into a byte array([]byte) again

b := []byte(???) // [77 255 129 3 1 1 7 82...] or M��
gob.NewDecoder(bytes.NewReader(b))

答案1

得分: 1

你可以在切片中使用构造函数语法。让我们来看一个例子:

array := []int{1, 2, 3}

在Python中的等效写法是:

array = [1, 2, 3]
英文:

You can use constructor syntax with slices. Lets show an example:

array := []int{1, 2, 3}

Equivalent in python is:

array = [1, 2, 3]

答案2

得分: 1

如果你的目标是创建一个带有测试数据的复合字面量,那么在fmt.Printf中使用%#v格式化动词:

buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
err := enc.Encode(<my struct>)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("var testData = %#v\n", buf.Bytes())  
// 输出 var testData = []byte{0x1f, 0xff, …

这段代码会打印一行文本你可以直接将其粘贴到 Go 源文件中

如果你的目标是重复使用编码后的数据请按照以下步骤进行

```go
buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
err := enc.Encode(<my struct>)
if err != nil {
    log.Fatal(err)
}
b := buf.Bytes() // 重复使用这个字节切片来创建解码器。



dec := gob.NewDecoder(bytes.NewReader(b))
// 使用解码器进行一些操作



// 在相同的数据上创建另一个解码器。
dec = gob.NewDecoder(bytes.NewReader(b))
英文:

If your goal is to create a composite literal with the test data, then use the %#v format verb in fmt.Printf:

buf := bytes.Buffer{}
enc := gob.NewEncoder(&amp;buf)
err := enc.Encode()
if err != nil {
	log.Fatal(err)
}
fmt.Printf(&quot;var testData = %#v\n&quot;, buf.Bytes())  
// prints var testData = []byte{0x1f, 0xff, …

This code prints a line of text that you can paste directly into a Go source file.

If your goal is to reuse the encoded data over and over, then do the following:

buf := bytes.Buffer{}
enc := gob.NewEncoder(&amp;buf)
err := enc.Encode(&lt;my struct&gt;)
if err != nil {
    log.Fatal(err)
}
b := buf.Bytes() // reuse this slice of bytes to create decoders.

⋮

dec := gob.NewDecoder(bytes.NewReader(b))
// do something with the decoder

⋮

// create another decoder on the same data.
dec = gob.NewDecoder(bytes.NewReader(b))

huangapple
  • 本文由 发表于 2022年1月29日 22:52:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/70906628.html
匿名

发表评论

匿名网友

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

确定