https://github.com/galaco/bitbuf
A simple Go Read & Write bitstream
https://github.com/galaco/bitbuf
bitstream buffer compression parser
Last synced: about 1 year ago
JSON representation
A simple Go Read & Write bitstream
- Host: GitHub
- URL: https://github.com/galaco/bitbuf
- Owner: Galaco
- Created: 2018-11-02T18:43:43.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-04-20T12:24:00.000Z (almost 5 years ago)
- Last Synced: 2025-01-30T09:44:20.870Z (about 1 year ago)
- Topics: bitstream, buffer, compression, parser
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://godoc.org/github.com/Galaco/bitbuf)
[](https://goreportcard.com/badge/github.com/galaco/bitbuf)
[](https://travis-ci.com/Galaco/bitbuf)
# bitbuf
A readable bitstream. Create from a byte slice, and read through the stream
bit by bit.
Supports the following read types:
* `byte`, `[]byte`
* `int8`, `int16`, `int32`, `int64`
* `uint8`, `uint16`, `uint32`, `uint64`
* `float32`, `float64`
* `string` (of known length, or until null terminator)
* `bits` (returned as `[]byte`
### Usage
```go
package main
import (
"bytes"
"encoding/binary"
"github.com/galaco/bitbuf"
"log"
)
type Foo struct {
A byte
B int16
C float32
D int64
E [32]byte
F uint8
G float64
H int8
I uint32
}
func main() {
dataBuffer := &bytes.Buffer{}
f := Foo{
A: 32,
B: 8375,
C: 2106.3212345,
D: 5635455352,
E: [32]byte{84,12,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,12,13,54,1,143,234,5,56,1,2},
F: 213,
G: -756351.123,
H: -57,
I: 12645123,
}
binary.Write(dataBuffer, binary.LittleEndian, f)
buf := bitbuf.NewReader(dataBuffer.Bytes())
log.Println(buf.ReadByte())
log.Println(buf.ReadInt16())
log.Println(buf.ReadFloat32())
log.Println(buf.ReadInt64())
log.Println(buf.ReadBytes(32))
log.Println(buf.ReadUint8())
log.Println(buf.ReadFloat64())
log.Println(buf.ReadInt8())
log.Println(buf.ReadUint32())
}
```