如何解决如何在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 举报,一经查实,本站将立刻删除。