binarypack

package
v0.46.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2024 License: GPL-3.0, BSD-3-Clause Imports: 6 Imported by: 0

README

Go BinaryPack

BinaryPack is a simple Golang library which implements some functionality of Python's struct package.

This one is forked from BinaryPack with more supported pad byte type

Format characters

Format characters (some characters like H have been reserved for future implementation of unsigned numbers):

    ? - bool, packed size 1 byte
    b - int8, packed size 1 bytes
    B - uint8, packed size 1 bytes
    h - int16, packed size 2 bytes
    H - uint16, packed size 2 bytes
    i, l - int32, packed size 4 bytes
    I, L - int32, packed size 4 bytes
    q - int64, packed size 8 bytes
    Q - uint64, packed size 8 bytes
    f - float32, packed size 4 bytes
    d - float64, packed size 8 bytes
    Ns - string, packed size N bytes, N is a number of runes to pack/unpack

Install

go get github.com/roman-kachanovsky/go-binary-pack/binary-pack

How to use

// Prepare format (slice of strings)
format := []string{"I", "?", "d", "6s"}

// Prepare values to pack
values := []interface{}{4, true, 3.14, "Golang"}

// Create BinaryPack object
bp := new(BinaryPack)

// Pack values to []byte
data, err := bp.Pack(format, values)

// Unpack binary data to []interface{}
unpacked_values, err := bp.UnPack(format, data)

// You can calculate size of expected binary data by format
size, err := bp.CalcSize(format)

Documentation

Overview

Package binary_pack performs conversions between some Go values represented as byte slices. This can be used in handling binary data stored in files or from network connections, among other sources. It uses format slices of strings as compact descriptions of the layout of the Go structs.

Format characters (some characters like H have been reserved for future implementation of unsigned numbers):

? - bool, packed size 1 byte
b - int8, packed size 1 bytes
B - uint8, packed size 1 bytes
h - int16, packed size 2 bytes
H - uint16, packed size 2 bytes
i, l - int32, packed size 4 bytes
I, L - int32, packed size 4 bytes
q - int64, packed size 8 bytes
Q - uint64, packed size 8 bytes
f - float32, packed size 4 bytes
d - float64, packed size 8 bytes
Ns - string, packed size N bytes, N is a number of runes to pack/unpack

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinaryPack

type BinaryPack struct{}

BinaryPack presents a BinaryPack

func New

func New() *BinaryPack

New create a new BinaryPack

func (*BinaryPack) CalcSize

func (bp *BinaryPack) CalcSize(format []string) (int, error)

CalcSize Returns the size of the struct (and hence of the byte slice) corresponding to the given format.

func (*BinaryPack) Pack

func (bp *BinaryPack) Pack(format []string, msg []interface{}) ([]byte, error)

Pack returns a byte slice containing the values of msg slice packed according to the given format. The items of msg slice must match the values required by the format exactly.

func (*BinaryPack) UnPack

func (bp *BinaryPack) UnPack(format []string, msg []byte) ([]interface{}, error)

UnPack the byte slice (presumably packed by Pack(format, msg)) according to the given format. The result is a []interface{} slice even if it contains exactly one item. The byte slice must contain not less the amount of data required by the format (len(msg) must more or equal CalcSize(format)).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL