Documentation ¶
Overview ¶
Package byteutils contains utilities for managing bytes.
Note that a byte's indices are [0, 8), and using an index beyond this range may cause a panic or undefined behavior.
Example ¶
Bitwise methods to manage bytes and their bits are abstracted with this library.
Functions are provided to "index" into a byte from the left and from the right.
var b byte = 0b0000_0000 byteutils.SetL(&b, 1) fmt.Printf("%08b\n", b) byteutils.ChangeR(&b, 0, byteutils.One) fmt.Printf("%08b\n", b) byteutils.ToggleL(&b, 7) fmt.Printf("%08b\n", b) fmt.Println(byteutils.GetR(b, 6))
Output: 01000000 01000001 01000000 1
Example (Endianness) ¶
Endianness determines the least and most significant bytes.
Little endian means that the last byte is the smallest. Big endian means that the last byte is the largest.
bytes := []byte{0x00, 0x01} littleEndianConversion := byteutils.ToUint16(bytes, byteutils.LittleEndian) bigEndianConversion := byteutils.ToUint16(bytes, byteutils.BigEndian) fmt.Printf("%d (0x%04X)\n", littleEndianConversion, littleEndianConversion) fmt.Printf("%d (0x%04X)\n", bigEndianConversion, bigEndianConversion)
Output: 1 (0x0001) 256 (0x0100)
Index ¶
- func BitAsBool(b Bit) bool
- func ChangeL(b *byte, index byte, bit Bit)
- func ChangeR(b *byte, index byte, bit Bit)
- func ClearL(b *byte, index byte)
- func ClearR(b *byte, index byte)
- func SetL(b *byte, index byte)
- func SetR(b *byte, index byte)
- func SliceL(b byte, i, end byte) byte
- func SliceR(b byte, i, end byte) byte
- func ToUint16(bytes Bytes, e Endian) uint16
- func ToUint32(bytes Bytes, e Endian) uint32
- func ToggleL(b *byte, index byte)
- func ToggleR(b *byte, index byte)
- type Bit
- type ByteIteratorFunc
- type Bytes
- type Endian
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SliceR ¶ added in v1.1.0
SliceR gets the [i, end) bits from the right.
i will be the "index" of the bit farthest to the right, and end is the index of the bit further left from the right.
Types ¶
type Bit ¶
type Bit = byte
Bit abstracts a single bit. The underlying type is byte instead of bool to make conversion usage with bytes simpler.
type ByteIteratorFunc ¶
ByteIteratorFunc takes a byte and the enumeration (count of calls to function). A pointer is passed so that the byte can be potentially modified.
type Bytes ¶
type Bytes = []byte
Bytes can be converted to and from other built-in types.
func BytesFromUint16 ¶
BytesFromUint16 creates bytes from a uint16.
func BytesFromUint32 ¶
BytesFromUint32 creates bytes from a uint32.
type Endian ¶
type Endian bool
Endian represents the endianness for conversion.
func (Endian) IterateSmallestToLargest ¶
func (e Endian) IterateSmallestToLargest(b Bytes, f ByteIteratorFunc)
IterateSmallestToLargest iterates from the smallest byte to the largest byte given the endianness. It will call the provided function on each byte.
func (Endian) IterateUint16 ¶
func (e Endian) IterateUint16(n uint16, f ByteIteratorFunc)
IterateUint16 iterates over a uint16 as bytes, from smallest to largest. Endianness determines if iteration goes from the left-most byte to the right-most (big endian), or the right-most byte to the left-most (little endian).
func (Endian) IterateUint32 ¶
func (e Endian) IterateUint32(n uint32, f ByteIteratorFunc)
IterateUint32 iterates over a uint32 as bytes, from smallest to largest. Endianness determines if iteration goes from the left-most byte to the right-most (big endian), or the right-most byte to the left-most (little endian).