https://github.com/pektezol/bitreader
The simplest bit reader library with big/little-endian support for Golang.
https://github.com/pektezol/bitreader
bitreader bitwise endianness go golang library
Last synced: 8 months ago
JSON representation
The simplest bit reader library with big/little-endian support for Golang.
- Host: GitHub
- URL: https://github.com/pektezol/bitreader
- Owner: pektezol
- License: lgpl-2.1
- Created: 2022-09-03T09:04:03.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-09-16T11:00:01.000Z (over 2 years ago)
- Last Synced: 2025-07-17T18:43:35.246Z (11 months ago)
- Topics: bitreader, bitwise, endianness, go, golang, library
- Language: Go
- Homepage: https://blog.pektezol.dev/posts/20230917_bitreader.html
- Size: 71.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# BitReader [](https://pkg.go.dev/github.com/pektezol/bitreader) [](https://goreportcard.com/report/github.com/pektezol/bitreader) [](https://github.com/pektezol/bitreader/blob/main/LICENSE)
The simplest bit reader with big/little-endian support for Golang.
## Installation
```bash
$ go get github.com/pektezol/bitreader
```
## Usage Examples
```go
import "github.com/pektezol/bitreader"
// ioStream: io.Reader Data to read from an io stream
// byteStream: []byte Data to read from a byte slice
// littleEndian: bool Little-endian(true) or big-endian(false) state
reader := bitreader.NewReader(ioStream, le)
reader := bitreader.NewReaderFromBytes(byteStream, le)
// Fork Reader, Copies Current Reader
newReader, err := reader.Fork()
// Read Total Number of Bits Left
bits, err := reader.ReadRemainingBits()
// Read First Bit
state, err := reader.ReadBool()
// Read Bits/Bytes
value, err := reader.ReadBits(64) // up to 64 bits
value, err := reader.ReadBytes(8) // up to 8 bytes
// Read String
text, err := reader.ReadString() // null-terminated
text, err := reader.ReadStringLength(256) // length-specified
// Read Bits/Bytes into Slice
arr, err := reader.ReadBitsToSlice(128)
arr, err := reader.ReadBytesToSlice(64)
// Skip Bits/Bytes
err := reader.SkipBits(8)
err := reader.SkipBytes(4)
// Wrapper functions
state := reader.TryReadBool() // bool
value := reader.TryReadInt1() // uint8
value := reader.TryReadUInt8() // uint8
value := reader.TryReadSInt8() // int8
value := reader.TryReadUInt16() // uint16
value := reader.TryReadSInt16() // int16
value := reader.TryReadUInt32() // uint32
value := reader.TryReadSInt32() // int32
value := reader.TryReadUInt64() // uint64
value := reader.TryReadSInt64() // int64
value := reader.TryReadFloat32() // float32
value := reader.TryReadFloat64() // float64
value := reader.TryReadBits(64) // uint64
value := reader.TryReadBytes(8) // uint64
text := reader.TryReadString() // string
text := reader.TryReadStringLength(64) // string
arr := reader.TryReadBitsToSlice(1024) // []byte
arr := reader.TryReadBytesToSlice(128) // []byte
bits := reader.TryReadRemainingBits() // uint64
```
## Error Handling
All ReadXXX(), SkipXXX() and Fork() functions returns an error message when they don't work as expected. It is advised to always handle errors. \
Wrapper functions, however, only returns the value and panics if an error is encountered, for the sake of ease of use.
## Bug Report / Feature Request
Using [Github Issues](https://github.com/pektezol/BitReader/issues/new/choose), you can report a bug that you encountered and/or request a feature that you would like to be added.
## Documentation
Full documentation can be found in https://pkg.go.dev/github.com/pektezol/bitreader
## License
This project is licensed under [GNU Lesser General Public License version 2.1](https://www.gnu.org/licenses/old-licenses/lgpl-2.1).