go - 如何在 Golang 中使用位打包将结构编码为二进制
问题描述
我正在尝试将大型数据结构编码为二进制。我为每个结构元素指定了位数。所以我需要根据位长将结构编码为二进制。标准 Golang 库编码/二进制将每个项目最小打包为一个字节。因此我需要另一种解决方案。如何在 Go 中将 struct 元素编码为指定的位数?
例如; 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
}
解决方案
推荐阅读
- r - 在水平图例标题的标题后添加换行符
- ios - 静态库的任务“:linkDebugTestIosX64”执行失败
- c# - IText 7对象引用未设置为对象的实例
- python - Pandas 删除多索引列的级别,保持第二级的第一列
- react-native - 存储在 react-native-fs 中的图像不显示
- html - 如何使用 grep 查找 html 标签的值
- reactjs - 如何更改从 redux 更改状态的颜色?
- python - 有没有可能从 mp4 文件路径获取仅获取 python 中的音频对象的方法?
- jquery - jquery选择更改事件发生和键盘导航
- amazon-web-services - 如何将 stepfunction executionId 解析为 SageMaker 批量转换作业名称?