datautil

package
v0.0.0-...-48f0ced Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: MIT Imports: 5 Imported by: 4

README

datautil 二进制数据工具库

这个库定义了二进制数据操作的标准并提供了相应方法

字节序

该库所有涉及到字节序的操作都使用大端字节序

编/解码

提供了部分Golang标准数据格式编/解码到[]byte的方法

支持的类型包括

  • bool
  • byte,uint8
  • int8
  • uint16
  • int16
  • uint32
  • int32
  • uint64
  • int64
  • float32
  • float64

其中,bool值编码为 byte(1) 和byte(0)。byte(0)代表false。解码时,非false的值代表true

数据片段

将一组[]byte , 编码为便于按顺序解析的带有自身长度的一串[]byte ,为了验证数据格式,可以可选的加入分隔符。

最后编码后的数据 格式为 长度标志(byte)|长度数据([]byte)|原始数据([]byte)|分隔符([]byte)

长度标志代表了后续长度数据的位数,对应关系如下:

  • <250:后续位数为0,使用长度标志作为数据长度
  • 255:后续位数为1(uint8)
  • 254:后续位数为2(uint16)
  • 253:后续位数为4(uint32)
  • 252:后续位数为8(uint64)
  • 251:保留

Documentation

Index

Constants

View Source
const PackLengthCodeNext2Byte = byte(254)
View Source
const PackLengthCodeNext4Byte = byte(253)
View Source
const PackLengthCodeNext8Byte = byte(252)
View Source
const PackLengthCodeNextByte = byte(255)
View Source
const PackLengthCodeReserved = byte(251)

Variables

View Source
var DataFalse = byte(0)
View Source
var DataTrue = byte(1)
View Source
var DelimiterZero = []byte{0}
View Source
var ErrDataLengthNotMatch = errors.New("binary data length not match")
View Source
var ErrDataLengthOverflow = errors.New("binary data length overflow")
View Source
var ErrDataTypeNotSupported = errors.New("binary data type not supported")
View Source
var ErrUnpackDataFail = errors.New("unpack binary data fail")

Functions

func Append

func Append(dst []byte, delimiter []byte, data ...[]byte) []byte

func Decode

func Decode(d []byte, data interface{}) error

func Decoder

func Decoder(values ...interface{}) herbdata.DataDecoder

func Encode

func Encode(data interface{}) ([]byte, error)

func Encoder

func Encoder(values ...interface{}) herbdata.DataEncoder

func Join

func Join(delimiter []byte, data ...[]byte) []byte

func JoinStrings

func JoinStrings(data ...string) string

func PackTo

func PackTo(w io.Writer, delimiter []byte, data ...[]byte) error

func UnpackFrom

func UnpackFrom(r io.Reader, delimiter []byte) ([]byte, error)

func WriteLengthBytes

func WriteLengthBytes(w io.Writer, length int) (int, error)

func WriteTo

func WriteTo(w io.Writer, delimiter []byte, data ...[]byte) (int, error)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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