Documentation
¶
Overview ¶
package go_cstruct provides functions for bit.
Index ¶
- Variables
- func BitsToBytes(b []bool, o binary.ByteOrder) []byte
- func BoolsToNumber(bools []bool) uint64
- func BytesToBits(b []byte, bitSize uint64, o binary.ByteOrder) ([]bool, error)
- func CheckAndEncode(ctx context.Context, order binary.ByteOrder, input interface{}) ([]byte, error)
- func Decode(buf []byte, order binary.ByteOrder, data interface{}) error
- func DecodeAndCheck(ctx context.Context, buf []byte, order binary.ByteOrder, data interface{}) error
- func Encode(order binary.ByteOrder, input interface{}) ([]byte, error)
- func GetBit(b []byte, off Offset, o binary.ByteOrder) (bool, error)
- func GetBitAsByte(b []byte, off Offset, o binary.ByteOrder) (byte, error)
- func GetBitAsByteNotShift(b []byte, off Offset, o binary.ByteOrder) (byte, error)
- func GetBits(bytes []byte, off Offset, bitSize uint64, o binary.ByteOrder) (ret []bool, err error)
- func GetBitsAsByte(bytes []byte, off Offset, bitSize uint64, o binary.ByteOrder) (ret []byte, err error)
- func GetBitsBitEndian(b []byte, o Offset, bitSize uint64, order binary.ByteOrder) ([]bool, error)
- func GetVals(bytes []byte, off Offset, v reflect.Value, o binary.ByteOrder) error
- func NewBits(size uint64, v bool) []bool
- func NumberToBools(number uint64, length int) []bool
- func SetBit(b []byte, off Offset, val bool, o binary.ByteOrder) error
- func SetBits(bytes []byte, off Offset, setBits []bool, o binary.ByteOrder) error
- func SetBitsBitEndian(b []byte, off Offset, setBits []bool, order binary.ByteOrder) error
- func SetVals(bytes []byte, off Offset, v reflect.Value, o binary.ByteOrder) error
- func SizeInByte(b []bool) int
- type Offset
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrOutOfRange = errors.New("out of range")
)
Functions ¶
func BitsToBytes ¶
BitsToBytes converts the unit. bit -> byte.
func BoolsToNumber ¶
func BytesToBits ¶
BytesToBits returns Bit slices. bitSize is the size of Bit slice.
func CheckAndEncode ¶
func Decode ¶
Decode reads structured binary data from i into data. Data must be a pointer to a fixed-size value. Not exported struct field is ignored.
Supports StructTag.
`cst:"skip"` : ignore the field. Skip X bits which is the size of the field. It is useful for reserved field.
`cst:"-"` : ignore the field. Offset is not changed.
func DecodeAndCheck ¶
func GetBit ¶
GetBit returns 1 or 0. GetBit reads b at Offset off, returns the bit.
Example ¶
package main
import (
"encoding/binary"
"fmt"
go_cstruct "github.com/piaoxue1949/go-cstruct"
)
func main() {
b := []byte{0x00, 0x80} /* 1000_0000 0000_0000 in bit */
off := go_cstruct.Offset{Byte: 0, Bit: 15}
ret, err := go_cstruct.GetBit(b, off, binary.LittleEndian)
if err != nil {
fmt.Printf("error:%s\n", err)
}
fmt.Printf("%t\n", ret)
}
Output: true
func GetBitAsByte ¶
GetBitAsByte returns byte (1 or 0). GetBitAsByte reads b at Offset off, returns the bit.
func GetBitAsByteNotShift ¶
GetBitAsByteNotShift reads b at Offset off, returns the bit. Return value is not bit shifted.
Example ¶
package main
import (
"encoding/binary"
"fmt"
go_cstruct "github.com/piaoxue1949/go-cstruct"
)
func main() {
b := []byte{0x00, 0x80} /* 1000_0000 0000_0000 in bit */
off := go_cstruct.Offset{Byte: 0, Bit: 15}
ret, err := go_cstruct.GetBitAsByteNotShift(b, off, binary.LittleEndian)
if err != nil {
fmt.Printf("error:%s\n", err)
}
fmt.Printf("0x%x\n", ret)
}
Output: 0x80
func GetBits ¶
GetBits returns Bit slice. GetBits reads bytes slice from Offset off. Read size is bitSize in bit.
func GetBitsAsByte ¶
func GetBitsAsByte(bytes []byte, off Offset, bitSize uint64, o binary.ByteOrder) (ret []byte, err error)
GetBitsAsByte returns byte slice. GetBitsAsByte reads bytes slice from Offset off. Read size is bitSize in bit.
Example ¶
package main
import (
"encoding/binary"
"fmt"
go_cstruct "github.com/piaoxue1949/go-cstruct"
)
func main() {
b := []byte{0x78} /* 0111_1000 in bit */
/* try to get 4bits(1111b) from 0111_1000 */
off := go_cstruct.Offset{Byte: 0, Bit: 3}
ret, err := go_cstruct.GetBitsAsByte(b, off, 4, binary.LittleEndian)
if err != nil {
fmt.Printf("error:%s\n", err)
}
fmt.Printf("0x%x\n", ret)
}
Output: 0x0f
func GetBitsBitEndian ¶
GetBitsBitEndian returns Bit slice. If order is LittleEndian, it is same as GetBits function. It respect bit order endianness when order is BigEndian.
func NumberToBools ¶
func SetBit ¶
SetBit sets bit on b at off. Bit is 0 if val == 0, 1 if val > 0. SetBit returns error if error occurred.
Example ¶
package main
import (
"encoding/binary"
"fmt"
go_cstruct "github.com/piaoxue1949/go-cstruct"
)
func main() {
b := []byte{0x00, 0x00} /* 0000_0000 0000_0000 in bit */
off := go_cstruct.Offset{Byte: 0, Bit: 15}
val := bool(true)
err := go_cstruct.SetBit(b, off, val, binary.LittleEndian)
if err != nil {
fmt.Printf("error:%s\n", err)
}
fmt.Printf("0x%x\n", b)
}
Output: 0x0080
func SetBits ¶
SetBits sets bits on bytes at off. The length to set is bitSize. SetBits returns error if error occurred.
Example ¶
package main
import (
"encoding/binary"
"fmt"
go_cstruct "github.com/piaoxue1949/go-cstruct"
)
func main() {
b := []byte{0x00, 0x00} /* 0000_0000 0000_0000 in bit */
off := go_cstruct.Offset{Byte: 0, Bit: 8}
val := []bool{false, false, false, true} /* 0000_1000 in bit */
err := go_cstruct.SetBits(b, off, val, binary.LittleEndian)
if err != nil {
fmt.Printf("error:%s\n", err)
}
fmt.Printf("0x%x\n", b)
}
Output: 0x0008
func SetBitsBitEndian ¶
SetBitsBitEndian sets bits in b. If order is LittleEndian, it is same as GetBits function. It respect bit order endianness when order is BigEndian.
func SizeInByte ¶
SizeInByte returns size of []bool slice in byte. e.g. It returns len([]bool) == 9.
Types ¶
type Offset ¶
Offset represents offset to access bits in byte slices.
func (Offset) Compare ¶
Compare returns an integer comparing two Offsets. The result will be 0 if off==b, -1 if off < b, and +1 if off > b.