gbinary

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package gbinary 提供了基本类型和字节切片之间的编码和解码功能。

Package gbinary 提供了基本类型和字节切片之间的编码和解码功能。

Package gbinary 提供了二进制数据处理功能。

Package gbinary 提供了基本类型和字节切片之间的编码和解码功能。 支持大端序和小端序两种字节序,提供了对整数、浮点数、布尔值、字符串等基本类型的编解码操作。 此外还包含了对二进制位操作的实验性功能。

基本用法:

// 编码示例
bytes := gbinary.Encode(123, "hello", true)

// 解码示例
var (
    num int
    str string
    boo bool
)
err := gbinary.Decode(bytes, &num, &str, &boo)

Package gbinary 提供了基本类型和字节切片之间的编码和解码功能。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeDecode

func BeDecode(b []byte, values ...interface{}) error

BeDecode 使用大端序将字节切片解码到指定的值中。 如果解码过程中发生错误,将返回包装后的错误信息。

func BeDecodeToBool

func BeDecodeToBool(b []byte) bool

BeDecodeToBool 将字节切片解码为布尔值。 如果切片为空或全为零值,返回false,否则返回true。

func BeDecodeToFloat32

func BeDecodeToFloat32(b []byte) float32

BeDecodeToFloat32 将大端序字节切片解码为float32类型的浮点数。

func BeDecodeToFloat64

func BeDecodeToFloat64(b []byte) float64

BeDecodeToFloat64 将大端序字节切片解码为float64类型的浮点数。

func BeDecodeToInt

func BeDecodeToInt(b []byte) int

BeDecodeToInt 将字节切片解码为int类型的整数。 根据字节切片的长度自动选择合适的解码方式。

func BeDecodeToInt16

func BeDecodeToInt16(b []byte) int16

BeDecodeToInt16 将大端序字节切片解码为int16类型的整数。

func BeDecodeToInt32

func BeDecodeToInt32(b []byte) int32

BeDecodeToInt32 将大端序字节切片解码为int32类型的整数。

func BeDecodeToInt64

func BeDecodeToInt64(b []byte) int64

BeDecodeToInt64 将大端序字节切片解码为int64类型的整数。

func BeDecodeToInt8

func BeDecodeToInt8(b []byte) int8

BeDecodeToInt8 将字节切片解码为int8类型的整数。 如果输入切片为空,将会panic。

func BeDecodeToString

func BeDecodeToString(b []byte) string

BeDecodeToString 将字节切片解码为字符串。

func BeDecodeToUint

func BeDecodeToUint(b []byte) uint

BeDecodeToUint 将字节切片解码为uint类型的无符号整数。 根据字节切片的长度自动选择合适的解码方式。

func BeDecodeToUint16

func BeDecodeToUint16(b []byte) uint16

BeDecodeToUint16 将大端序字节切片解码为uint16类型的无符号整数。

func BeDecodeToUint32

func BeDecodeToUint32(b []byte) uint32

BeDecodeToUint32 将大端序字节切片解码为uint32类型的无符号整数。

func BeDecodeToUint64

func BeDecodeToUint64(b []byte) uint64

BeDecodeToUint64 将大端序字节切片解码为uint64类型的无符号整数。

func BeDecodeToUint8

func BeDecodeToUint8(b []byte) uint8

BeDecodeToUint8 将字节切片解码为uint8类型的无符号整数。 如果输入切片为空,将会panic。

func BeEncode

func BeEncode(values ...interface{}) []byte

BeEncode 使用大端序将一个或多个值编码为字节切片。 通过类型断言检查values中每个值的类型,并调用相应的转换函数进行字节转换。 支持常见变量类型的断言,对于不支持的类型,使用fmt.Sprintf将值转换为字符串后再转换为字节。

func BeEncodeBool

func BeEncodeBool(b bool) []byte

BeEncodeBool 将布尔值编码为字节切片。 true编码为[1],false编码为[0]。

func BeEncodeByLength

func BeEncodeByLength(length int, values ...interface{}) []byte

BeEncodeByLength 使用大端序将值编码为指定长度的字节切片。 如果编码结果长度小于指定长度,则在末尾补零;如果大于指定长度,则截断。

func BeEncodeFloat32

func BeEncodeFloat32(f float32) []byte

BeEncodeFloat32 将float32类型的浮点数编码为大端序字节切片。

func BeEncodeFloat64

func BeEncodeFloat64(f float64) []byte

BeEncodeFloat64 将float64类型的浮点数编码为大端序字节切片。

func BeEncodeInt

func BeEncodeInt(i int) []byte

BeEncodeInt 将int类型的整数编码为字节切片。 根据整数大小自动选择合适的字节长度进行编码。

func BeEncodeInt16

func BeEncodeInt16(i int16) []byte

BeEncodeInt16 将int16类型的整数编码为大端序字节切片。

func BeEncodeInt32

func BeEncodeInt32(i int32) []byte

BeEncodeInt32 将int32类型的整数编码为大端序字节切片。

func BeEncodeInt64

func BeEncodeInt64(i int64) []byte

BeEncodeInt64 将int64类型的整数编码为大端序字节切片。

func BeEncodeInt8

func BeEncodeInt8(i int8) []byte

BeEncodeInt8 将int8类型的整数编码为字节切片。

func BeEncodeString

func BeEncodeString(s string) []byte

BeEncodeString 将字符串编码为字节切片。

func BeEncodeUint

func BeEncodeUint(i uint) []byte

BeEncodeUint 将uint类型的无符号整数编码为字节切片。 根据整数大小自动选择合适的字节长度进行编码。

func BeEncodeUint16

func BeEncodeUint16(i uint16) []byte

BeEncodeUint16 将uint16类型的无符号整数编码为大端序字节切片。

func BeEncodeUint32

func BeEncodeUint32(i uint32) []byte

BeEncodeUint32 将uint32类型的无符号整数编码为大端序字节切片。

func BeEncodeUint64

func BeEncodeUint64(i uint64) []byte

BeEncodeUint64 将uint64类型的无符号整数编码为大端序字节切片。

func BeEncodeUint8

func BeEncodeUint8(i uint8) []byte

BeEncodeUint8 将uint8类型的无符号整数编码为字节切片。

func BeFillUpSize

func BeFillUpSize(b []byte, l int) []byte

BeFillUpSize 使用大端序将字节切片b填充到指定长度l。 注意:该函数会创建一个新的字节切片并复制原始数据,以避免修改原始参数。

func Decode

func Decode(b []byte, values ...interface{}) error

Decode 将字节切片解码到指定的值中。 使用小端序进行解码。如果解码过程中发生错误,将返回相应的错误。

func DecodeBits

func DecodeBits(bits []Bit) int

DecodeBits 将二进制位数组解码为整数。

func DecodeBitsToUint

func DecodeBitsToUint(bits []Bit) uint

DecodeBitsToUint 将二进制位数组解码为无符号整数。

func DecodeToBool

func DecodeToBool(b []byte) bool

DecodeToBool 将字节切片解码为布尔值。 使用小端序解码。

func DecodeToFloat32

func DecodeToFloat32(b []byte) float32

DecodeToFloat32 将字节切片解码为float32类型的浮点数。 使用小端序解码。

func DecodeToFloat64

func DecodeToFloat64(b []byte) float64

DecodeToFloat64 将字节切片解码为float64类型的浮点数。 使用小端序解码。

func DecodeToInt

func DecodeToInt(b []byte) int

DecodeToInt 将字节切片解码为int类型的整数。 使用小端序解码。

func DecodeToInt16

func DecodeToInt16(b []byte) int16

DecodeToInt16 将字节切片解码为int16类型的整数。 使用小端序解码。

func DecodeToInt32

func DecodeToInt32(b []byte) int32

DecodeToInt32 将字节切片解码为int32类型的整数。 使用小端序解码。

func DecodeToInt64

func DecodeToInt64(b []byte) int64

DecodeToInt64 将字节切片解码为int64类型的整数。 使用小端序解码。

func DecodeToInt8

func DecodeToInt8(b []byte) int8

DecodeToInt8 将字节切片解码为int8类型的整数。 使用小端序解码。

func DecodeToString

func DecodeToString(b []byte) string

DecodeToString 将字节切片解码为字符串。 使用小端序解码。

func DecodeToUint

func DecodeToUint(b []byte) uint

DecodeToUint 将字节切片解码为uint类型的无符号整数。 使用小端序解码。

func DecodeToUint16

func DecodeToUint16(b []byte) uint16

DecodeToUint16 将字节切片解码为uint16类型的无符号整数。 使用小端序解码。

func DecodeToUint32

func DecodeToUint32(b []byte) uint32

DecodeToUint32 将字节切片解码为uint32类型的无符号整数。 使用小端序解码。

func DecodeToUint64

func DecodeToUint64(b []byte) uint64

DecodeToUint64 将字节切片解码为uint64类型的无符号整数。 使用小端序解码。

func DecodeToUint8

func DecodeToUint8(b []byte) uint8

DecodeToUint8 将字节切片解码为uint8类型的无符号整数。 使用小端序解码。

func Encode

func Encode(values ...interface{}) []byte

Encode 将任意数量的值编码为字节切片。 使用小端序进行编码。

func EncodeBitsToBytes

func EncodeBitsToBytes(bits []Bit) []byte

EncodeBitsToBytes 将二进制位数组编码为字节切片。 从左到右编码,如果位数不足8的倍数,末尾补0。

func EncodeBool

func EncodeBool(b bool) []byte

EncodeBool 将布尔值编码为字节切片。 使用小端序编码。

func EncodeByLength

func EncodeByLength(length int, values ...interface{}) []byte

EncodeByLength 将指定数量的值编码为字节切片。 返回的字节切片长度由参数length指定。使用小端序编码。

func EncodeFloat32

func EncodeFloat32(f float32) []byte

EncodeFloat32 将float32类型的浮点数编码为字节切片。 使用小端序编码。

func EncodeFloat64

func EncodeFloat64(f float64) []byte

EncodeFloat64 将float64类型的浮点数编码为字节切片。 使用小端序编码。

func EncodeInt

func EncodeInt(i int) []byte

EncodeInt 将int类型的整数编码为字节切片。 使用小端序编码。

func EncodeInt16

func EncodeInt16(i int16) []byte

EncodeInt16 将int16类型的整数编码为字节切片。 使用小端序编码。

func EncodeInt32

func EncodeInt32(i int32) []byte

EncodeInt32 将int32类型的整数编码为字节切片。 使用小端序编码。

func EncodeInt64

func EncodeInt64(i int64) []byte

EncodeInt64 将int64类型的整数编码为字节切片。 使用小端序编码。

func EncodeInt8

func EncodeInt8(i int8) []byte

EncodeInt8 将int8类型的整数编码为字节切片。 使用小端序编码。

func EncodeString

func EncodeString(s string) []byte

EncodeString 将字符串编码为字节切片。 使用小端序编码。

func EncodeUint

func EncodeUint(i uint) []byte

EncodeUint 将uint类型的无符号整数编码为字节切片。 使用小端序编码。

func EncodeUint16

func EncodeUint16(i uint16) []byte

EncodeUint16 将uint16类型的无符号整数编码为字节切片。 使用小端序编码。

func EncodeUint32

func EncodeUint32(i uint32) []byte

EncodeUint32 将uint32类型的无符号整数编码为字节切片。 使用小端序编码。

func EncodeUint64

func EncodeUint64(i uint64) []byte

EncodeUint64 将uint64类型的无符号整数编码为字节切片。 使用小端序编码。

func EncodeUint8

func EncodeUint8(i uint8) []byte

EncodeUint8 将uint8类型的无符号整数编码为字节切片。 使用小端序编码。

func LeDecode

func LeDecode(b []byte, values ...interface{}) error

LeDecode 使用小端序将字节切片解码到指定的值中。 如果解码过程中发生错误,将返回包装后的错误信息。

func LeDecodeToBool

func LeDecodeToBool(b []byte) bool

LeDecodeToBool 将字节切片解码为布尔值。 如果切片为空或全为零值,返回false,否则返回true。

func LeDecodeToFloat32

func LeDecodeToFloat32(b []byte) float32

LeDecodeToFloat32 将小端序字节切片解码为float32类型的浮点数。

func LeDecodeToFloat64

func LeDecodeToFloat64(b []byte) float64

LeDecodeToFloat64 将小端序字节切片解码为float64类型的浮点数。

func LeDecodeToInt

func LeDecodeToInt(b []byte) int

LeDecodeToInt 将字节切片解码为int类型的整数。 根据字节切片的长度自动选择合适的解码方式。

func LeDecodeToInt16

func LeDecodeToInt16(b []byte) int16

LeDecodeToInt16 将小端序字节切片解码为int16类型的整数。

func LeDecodeToInt32

func LeDecodeToInt32(b []byte) int32

LeDecodeToInt32 将小端序字节切片解码为int32类型的整数。

func LeDecodeToInt64

func LeDecodeToInt64(b []byte) int64

LeDecodeToInt64 将小端序字节切片解码为int64类型的整数。

func LeDecodeToInt8

func LeDecodeToInt8(b []byte) int8

LeDecodeToInt8 将字节切片解码为int8类型的整数。 如果输入切片为空,将会panic。

func LeDecodeToString

func LeDecodeToString(b []byte) string

LeDecodeToString 将字节切片解码为字符串。

func LeDecodeToUint

func LeDecodeToUint(b []byte) uint

LeDecodeToUint 将字节切片解码为uint类型的无符号整数。 根据字节切片的长度自动选择合适的解码方式。

func LeDecodeToUint16

func LeDecodeToUint16(b []byte) uint16

LeDecodeToUint16 将小端序字节切片解码为uint16类型的无符号整数。

func LeDecodeToUint32

func LeDecodeToUint32(b []byte) uint32

LeDecodeToUint32 将小端序字节切片解码为uint32类型的无符号整数。

func LeDecodeToUint64

func LeDecodeToUint64(b []byte) uint64

LeDecodeToUint64 将小端序字节切片解码为uint64类型的无符号整数。

func LeDecodeToUint8

func LeDecodeToUint8(b []byte) uint8

LeDecodeToUint8 将字节切片解码为uint8类型的无符号整数。 如果输入切片为空,将会panic。

func LeEncode

func LeEncode(values ...interface{}) []byte

LeEncode 使用小端序将一个或多个值编码为字节切片。 通过类型断言检查values中每个值的类型,并调用相应的转换函数进行字节转换。 支持常见变量类型的断言,对于不支持的类型,使用fmt.Sprintf将值转换为字符串后再转换为字节。

func LeEncodeBool

func LeEncodeBool(b bool) []byte

LeEncodeBool 将布尔值编码为字节切片。 true编码为[1],false编码为[0]。

func LeEncodeByLength

func LeEncodeByLength(length int, values ...interface{}) []byte

LeEncodeByLength 使用小端序将值编码为指定长度的字节切片。 如果编码结果长度小于指定长度,则在末尾补零;如果大于指定长度,则截断。

func LeEncodeFloat32

func LeEncodeFloat32(f float32) []byte

LeEncodeFloat32 将float32类型的浮点数编码为小端序字节切片。

func LeEncodeFloat64

func LeEncodeFloat64(f float64) []byte

LeEncodeFloat64 将float64类型的浮点数编码为小端序字节切片。

func LeEncodeInt

func LeEncodeInt(i int) []byte

LeEncodeInt 将int类型的整数编码为字节切片。 根据整数大小自动选择合适的字节长度进行编码。

func LeEncodeInt16

func LeEncodeInt16(i int16) []byte

LeEncodeInt16 将int16类型的整数编码为小端序字节切片。

func LeEncodeInt32

func LeEncodeInt32(i int32) []byte

LeEncodeInt32 将int32类型的整数编码为小端序字节切片。

func LeEncodeInt64

func LeEncodeInt64(i int64) []byte

LeEncodeInt64 将int64类型的整数编码为小端序字节切片。

func LeEncodeInt8

func LeEncodeInt8(i int8) []byte

LeEncodeInt8 将int8类型的整数编码为字节切片。

func LeEncodeString

func LeEncodeString(s string) []byte

LeEncodeString 将字符串编码为字节切片。

func LeEncodeUint

func LeEncodeUint(i uint) []byte

LeEncodeUint 将uint类型的无符号整数编码为字节切片。 根据整数大小自动选择合适的字节长度进行编码。

func LeEncodeUint16

func LeEncodeUint16(i uint16) []byte

LeEncodeUint16 将uint16类型的无符号整数编码为小端序字节切片。

func LeEncodeUint32

func LeEncodeUint32(i uint32) []byte

LeEncodeUint32 将uint32类型的无符号整数编码为小端序字节切片。

func LeEncodeUint64

func LeEncodeUint64(i uint64) []byte

LeEncodeUint64 将uint64类型的无符号整数编码为小端序字节切片。

func LeEncodeUint8

func LeEncodeUint8(i uint8) []byte

LeEncodeUint8 将uint8类型的无符号整数编码为字节切片。

func LeFillUpSize

func LeFillUpSize(b []byte, l int) []byte

LeFillUpSize 使用小端序将字节切片b填充到指定长度l。 注意:该函数会创建一个新的字节切片并复制原始数据,以避免修改原始参数。

Types

type Bit

type Bit int8

Bit 表示二进制位(0或1)。

func DecodeBytesToBits

func DecodeBytesToBits(bs []byte) []Bit

DecodeBytesToBits 将字节切片解析为二进制位数组。 每个字节被解析为8个二进制位。

func EncodeBits

func EncodeBits(bits []Bit, i int, l int) []Bit

EncodeBits 将整数编码为指定长度的二进制位数组。 本质上是调用EncodeBitsWithUint,将int转换为uint进行处理。

func EncodeBitsWithUint

func EncodeBitsWithUint(bits []Bit, ui uint, l int) []Bit

EncodeBitsWithUint 将无符号整数按位编码到二进制位数组中。 bits: 现有的位数组,如果为nil则创建新数组 ui: 要编码的无符号整数 l: 编码后占用的位数 返回包含编码结果的位数组

Jump to

Keyboard shortcuts

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