Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hymkor/go-bitfield
The C-like Bitfield packing/unpacking library on Golang
https://github.com/hymkor/go-bitfield
bitfield bitfields go golang reflect
Last synced: 15 days ago
JSON representation
The C-like Bitfield packing/unpacking library on Golang
- Host: GitHub
- URL: https://github.com/hymkor/go-bitfield
- Owner: hymkor
- License: mit
- Created: 2023-01-23T13:41:13.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-23T16:10:09.000Z (almost 2 years ago)
- Last Synced: 2024-10-31T00:07:57.874Z (2 months ago)
- Topics: bitfield, bitfields, go, golang, reflect
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
go-bitfield
===========- `Pack` and `Unpack` :
- convert between uint64 and structure
- `PackInline` and `UnpackInline` :
- convert between uint64 and array.```go
package bitfield_testimport (
"testing""github.com/hymkor/go-bitfield"
)const packedValue = 0x7423
type DosDate struct {
Second int `bit:"5"`
Min uint `bit:"6"`
Hour int `bit:"5"`
}func TestUnpack(t *testing.T) {
var dt DosDate
if err := bitfield.Unpack(uint64(packedValue), &dt); err != nil {
t.Fatal(err.Error())
return
}
if dt.Second != 3 {
t.Fatalf("Second: expect 3, but %d", dt.Second)
}
if dt.Min != 33 {
t.Fatalf("Min: expect 33, but %d", dt.Min)
}
if dt.Hour != 14 {
t.Fatalf("Hour: expect 14,but %d", dt.Hour)
}
}func TestPack(t *testing.T) {
var dt = &DosDate{
Second: 3,
Min: 33,
Hour: 14,
}value, err := bitfield.Pack(dt)
if err != nil {
t.Fatalf("pack: %s", err.Error())
}
if value != packedValue {
t.Fatalf("pack: expect %d, but %d", packedValue, value)
}
}const (
secondBit = 5
minBit = 6
hourBit = 5
)func TestUnpackInline(t *testing.T) {
dt := bitfield.UnpackInline(packedValue, secondBit, minBit, hourBit)
if dt[0] != 3 {
t.Fatalf("Second: expect 3, but %d", dt[0])
}
if dt[1] != 33 {
t.Fatalf("Min: expect 33, but %d", dt[1])
}
if dt[2] != 14 {
t.Fatalf("Hour: expect 14,but %d", dt[2])
}
}func TestPackInline(t *testing.T) {
dt := bitfield.PackInline(secondBit, 3, minBit, 33, hourBit, 14)
if dt != packedValue {
t.Fatalf("PackInline expect %d,but %d", packedValue, dt)
}
}
```