将 []uint32 转换为 []byte,以及在 golang 中进行相反的转换。

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

Convert []uint32 to []byte and vice versa in golang

问题

在Golang中,将[]uint32转换为[]byte并从[]byte转换回[]uint32的最高效的方法(性能方面)是什么?

例如:

func main() {
   source := []uint32{1,2,3}
   dest := make([]byte, 4 * len(source))
   // source to dest
   // ?
   check := len(dest)/4
   // dest to check
   // ?
}

我有一个解决方案,但它包含了除法、取模和乘法操作。

package main
import (
    "fmt"
)
func main() {
    source := []uint32{1,2,3}
    dest := make([]byte, 4*len(source))
    fmt.Println(source)
    for start, v := range source {
       dest[start*4+0] = byte(v % 256)
       dest[start*4+1] = byte(v / 256 % 256)
       dest[start*4+2] = byte(v / 256 / 256 % 256)
       dest[start*4+3] = byte(v / 256/ 256/ 256% 256)
    }
    fmt.Println(dest)
    check := make([]uint32,cap(dest)/4)
    for start := 0; start<len(check); start++ {
       check[start] = uint32(dest[start*4+0]) + uint32(dest[start*4+1]) * 256 + uint32(dest[start*4+2]) * 256 * 256 + uint32(dest[start*4+3]) * 256 * 256 * 256
    }  
    fmt.Println(check)
}
英文:

What's the most efficient way (in performance) to convert []uint32 to and from []byte in Golang?

for example:

func main() {
   source := []uint32{1,2,3}
   dest := make([]byte, 4 * len(source))
   // source to dest
   // ?
   check := len(dest)/4
   // dest to check
   // ?
}

I have a solution but it consist div, mod, and multiply

package main
import (
    &quot;fmt&quot;
)
func main() {
    source := []uint32{1,2,3}
    dest := make([]byte, 4*len(source))
    fmt.Println(source)
    for start, v := range source {
       dest[start*4+0] = byte(v % 256)
       dest[start*4+1] = byte(v / 256 % 256)
       dest[start*4+2] = byte(v / 256 / 256 % 256)
       dest[start*4+3] = byte(v / 256/ 256/ 256% 256)
    }
    fmt.Println(dest)
    check := make([]uint32,cap(dest)/4)
    for start := 0; start&lt;len(check); start++ {
       check[start] = uint32(dest[start*4+0]) + uint32(dest[start*4+1]) * 256 + uint32(dest[start*4+2]) * 256 * 256 + uint32(dest[start*4+3]) * 256 * 256 * 256
    }  
    fmt.Println(check)
}

答案1

得分: 9

我怀疑你想要这样的东西 Playground

根据需要将 LittleEndian 调整为 BigEndian

package main

import (
	"bytes"
	"encoding/binary"
	"fmt"
)

func main() {
	buf := new(bytes.Buffer)
	source := []uint32{1, 2, 3}
	err := binary.Write(buf, binary.LittleEndian, source)
	if err != nil {
		fmt.Println("binary.Write failed:", err)
	}
	fmt.Printf("Encoded: % x\n", buf.Bytes())

	check := make([]uint32, 3)
	rbuf := bytes.NewReader(buf.Bytes())
	err = binary.Read(rbuf, binary.LittleEndian, &check)
	if err != nil {
		fmt.Println("binary.Read failed:", err)
	}
	fmt.Printf("Decoded: %v\n", check)

}
英文:

I suspect you are after something like this Playground

Adjust LittleEndian for BigEndian as appropriate

package main

import (
	&quot;bytes&quot;
	&quot;encoding/binary&quot;
	&quot;fmt&quot;
)

func main() {
	buf := new(bytes.Buffer)
	source := []uint32{1, 2, 3}
	err := binary.Write(buf, binary.LittleEndian, source)
	if err != nil {
		fmt.Println(&quot;binary.Write failed:&quot;, err)
	}
	fmt.Printf(&quot;Encoded: % x\n&quot;, buf.Bytes())

	check := make([]uint32, 3)
	rbuf := bytes.NewReader(buf.Bytes())
	err = binary.Read(rbuf, binary.LittleEndian, &amp;check)
	if err != nil {
		fmt.Println(&quot;binary.Read failed:&quot;, err)
	}
	fmt.Printf(&quot;Decoded: %v\n&quot;, check)

}

huangapple
  • 本文由 发表于 2016年4月5日 22:53:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/36429885.html
匿名

发表评论

匿名网友

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

确定