Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zhuangsirui/binpacker
A binary stream packer and unpacker
https://github.com/zhuangsirui/binpacker
packer unpacker
Last synced: about 2 months ago
JSON representation
A binary stream packer and unpacker
- Host: GitHub
- URL: https://github.com/zhuangsirui/binpacker
- Owner: zhuangsirui
- License: mit
- Created: 2016-02-02T10:06:11.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-10-08T04:16:12.000Z (about 3 years ago)
- Last Synced: 2024-07-31T20:45:37.431Z (4 months ago)
- Topics: packer, unpacker
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 218
- Watchers: 14
- Forks: 37
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - binpacker - Binary packer and unpacker helps user build custom binary stream. (Data Structures and Algorithms / Bit-packing and Compression)
- awesome-go - binpacker - Binary packer and unpacker helps user build custom binary stream. (Data Structures and Algorithms / Bit-packing and Compression)
- awesome-go - binpacker - A binary stream packer and unpacker - ★ 98 (Data Structures)
- awesome-go-extra - binpacker - 02-02T10:06:11Z|2021-10-08T04:16:12Z| (Generators / Bit-packing and Compression)
README
# binpacker [![Build Status](https://travis-ci.org/zhuangsirui/binpacker.svg?branch=master)](https://travis-ci.org/zhuangsirui/binpacker) [![GoDoc](https://godoc.org/github.com/zhuangsirui/binpacker?status.svg)](https://godoc.org/github.com/zhuangsirui/binpacker) [![Go Report Card](https://goreportcard.com/badge/github.com/zhuangsirui/binpacker)](https://goreportcard.com/report/github.com/zhuangsirui/binpacker)
A binary packer and unpacker.# Install
```bash
go get github.com/zhuangsirui/binpacker
```# Examples
## Packer
```go
buffer := new(bytes.Buffer)
packer := binpacker.NewPacker(binary.BigEndian, buffer)
packer.PushByte(0x01)
packer.PushBytes([]byte{0x02, 0x03})
packer.PushUint16(math.MaxUint16)
``````go
// You can push data like this
buffer := new(bytes.Buffer)
packer := binpacker.NewPacker(binary.BigEndian, buffer)
packer.PushByte(0x01).PushBytes([]byte{0x02, 0x03}).PushUint16(math.MaxUint16)
packer.Error() // Make sure error is nil
```## Unpacker
**Example data**
```go
buffer := new(bytes.Buffer)
packer := binpacker.NewPacker(binary.BigEndian, buffer)
unpacker := binpacker.NewUnpacker(binary.BigEndian, buffer)
packer.PushByte(0x01)
packer.PushUint16(math.MaxUint16)
``````go
var val1 byte
var val2 uint16
var err error
val1, err = unpacker.ShiftByte()
val2, err = unpacker.ShiftUint16()
``````go
var val1 byte
var val2 uint16
var err error
unpacker.FetchByte(&val1).FetchUint16(&val2)
unpacker.Error() // Make sure error is nil
```