如何在Golang中使用位打包将结构编码为二进制

问题描述

我正在尝试将大型数据结构编码为二进制。我为每个结构元素指定了位数。所以我需要根据位长将结构编码为二进制。标准Golang库Encoding/binary至少将每个项目打包为一个字节。因此,我需要另一个解决方案。如何在Go中将结构元素编码为指定的位数?

例如; Item1 = 00001101 Item2 = 00000110结果将为01101110

type Elements struct{
    Item1 uint8  // number of bits = 5
    Item2 uint8  // number of bits = 3
    Item3 uint8  // number of bits = 2
    Item4 uint64 // number of bits = 60
    Item5 uint16 // number of bits = 11
    Item6 []byte // bit length = 8
    Item7 Others
}
type Others struct{
    Other1 uint8  // number of bits = 4
    Other2 uint32 // number of bits = 21
    Other3 uint16 // number of bits = 9
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)