Introduction

The GoFrame framework provides an independent binary data operation package gbinary, which is mainly used for mutual conversion between various data types and []byte binary types; it also provides precise bit processing functions for integer data. It is commonly used for data encoding/decoding during network communication and data file operations.

Usage:

  1. import "github.com/gogf/gf/v2/encoding/gbinary"

Interface Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/encoding/gbinary

The interface documentation for binary data structure conversion processing is as follows:

  1. func Encode(vs ...interface{}) ([]byte, error)
  2. func EncodeInt(i int) []byte
  3. func EncodeInt8(i int8) []byte
  4. func EncodeInt16(i int16) []byte
  5. func EncodeInt32(i int32) []byte
  6. func EncodeInt64(i int64) []byte
  7. func EncodeUint(i uint) []byte
  8. func EncodeUint8(i uint8) []byte
  9. func EncodeUint16(i uint16) []byte
  10. func EncodeUint32(i uint32) []byte
  11. func EncodeUint64(i uint64) []byte
  12. func EncodeBool(b bool) []byte
  13. func EncodeFloat32(f float32) []byte
  14. func EncodeFloat64(f float64) []byte
  15. func EncodeString(s string) []byte
  16. func Decode(b []byte, vs ...interface{}) error
  17. func DecodeToInt(b []byte) int
  18. func DecodeToInt8(b []byte) int8
  19. func DecodeToInt16(b []byte) int16
  20. func DecodeToInt32(b []byte) int32
  21. func DecodeToInt64(b []byte) int64
  22. func DecodeToUint(b []byte) uint
  23. func DecodeToUint8(b []byte) uint8
  24. func DecodeToUint16(b []byte) uint16
  25. func DecodeToUint32(b []byte) uint32
  26. func DecodeToUint64(b []byte) uint64
  27. func DecodeToBool(b []byte) bool
  28. func DecodeToFloat32(b []byte) float32
  29. func DecodeToFloat64(b []byte) float64
  30. func DecodeToString(b []byte) string

The interface documentation for bit-level processing support is as follows:

  1. func EncodeBits(bits []Bit, i int, l int) []Bit
  2. func EncodeBitsWithUint(bits []Bit, ui uint, l int) []Bit
  3. func EncodeBitsToBytes(bits []Bit) []byte
  4. func DecodeBits(bits []Bit) uint
  5. func DecodeBitsToUint(bits []Bit) uint
  6. func DecodeBytesToBits(bs []byte) []Bit

The Bit type represents a binary digit (0 or 1), defined as follows:

  1. type Bit int8

Usage Example

Let’s look at a comprehensive example of binary operations, demonstrating most binary conversion operations.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/encoding/gbinary"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/os/glog"
  7. )
  8. func main() {
  9. // Use gbinary.Encode to perform binary packing on basic data types
  10. if buffer := gbinary.Encode(18, 300, 1.01); buffer != nil {
  11. // glog.Error(err)
  12. } else {
  13. fmt.Println(buffer)
  14. }
  15. // Use gbinary.Decode for integer binary unpacking. Note that the second and subsequent parameters are pointers to fixed-length integer variables.
  16. // Examples include: int8/16/32/64, uint8/16/32/64, float32/64
  17. // 1.01 here defaults to float64 type (on a 64-bit system)
  18. if buffer := gbinary.Encode(18, 300, 1.01); buffer != nil {
  19. // glog.Error(err)
  20. } else {
  21. var i1 int8
  22. var i2 int16
  23. var f3 float64
  24. if err := gbinary.Decode(buffer, &i1, &i2, &f3); err != nil {
  25. glog.Error(gctx.New(), err)
  26. } else {
  27. fmt.Println(i1, i2, f3)
  28. }
  29. }
  30. // Encode/Decode int, automatically recognize variable length
  31. fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(1)))
  32. fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(300)))
  33. fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(70000)))
  34. fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(2000000000)))
  35. fmt.Println(gbinary.DecodeToInt(gbinary.EncodeInt(500000000000)))
  36. // Encode/Decode uint, automatically recognize variable length
  37. fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(1)))
  38. fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(300)))
  39. fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(70000)))
  40. fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(2000000000)))
  41. fmt.Println(gbinary.DecodeToUint(gbinary.EncodeUint(500000000000)))
  42. // Encode/Decode int8/16/32/64
  43. fmt.Println(gbinary.DecodeToInt8(gbinary.EncodeInt8(int8(100))))
  44. fmt.Println(gbinary.DecodeToInt16(gbinary.EncodeInt16(int16(100))))
  45. fmt.Println(gbinary.DecodeToInt32(gbinary.EncodeInt32(int32(100))))
  46. fmt.Println(gbinary.DecodeToInt64(gbinary.EncodeInt64(int64(100))))
  47. // Encode/Decode uint8/16/32/64
  48. fmt.Println(gbinary.DecodeToUint8(gbinary.EncodeUint8(uint8(100))))
  49. fmt.Println(gbinary.DecodeToUint16(gbinary.EncodeUint16(uint16(100))))
  50. fmt.Println(gbinary.DecodeToUint32(gbinary.EncodeUint32(uint32(100))))
  51. fmt.Println(gbinary.DecodeToUint64(gbinary.EncodeUint64(uint64(100))))
  52. // Encode/Decode string
  53. fmt.Println(gbinary.DecodeToString(gbinary.EncodeString("I'm string!")))
  54. }

The result of the above program execution is:

  1. [18 44 1 41 92 143 194 245 40 240 63]
  2. 18 300 1.01
  3. 1
  4. 300
  5. 70000
  6. 2000000000
  7. 500000000000
  8. 1
  9. 300
  10. 70000
  11. 2000000000
  12. 500000000000
  13. 100
  14. 100
  15. 100
  16. 100
  17. 100
  18. 100
  19. 100
  20. 100
  21. I'm string!
  1. Encoding

The gbinary.Encode method is a very powerful and flexible method that can convert all basic types to binary type ([]byte). Inside the gbinary.Encode method, the variable length is calculated automatically, using the smallest binary length to store the binary value of the variable. For example, for an int type variable with a value of 1, gbinary.Encode will use only 1 byte to store it, while a variable with a value of 300 will use 2 bytes to store it, minimizing the storage space of the binary result. Therefore, when parsing, pay great attention to the length of []byte. It is recommended to use fixed-length basic types like int8/16/32/64 when encoding/decoding binary to ensure the correct variable form is used during parsing, reducing the chance of errors.

The gbinary package also provides a series of gbinary.Encode* methods for converting basic data types to binary. In particular, gbinary.EncodeInt/gbinary.EncodeUint internally recognizes the variable value size and returns a variable-length []byte value, with a length range of 1/2/4/8.

  1. Decoding

In binary type parsing operations, the length of the binary ([]byte length) is crucial. Only with the correct length can correct parsing be executed. Therefore, the length of the variables given to gbinary.Decode must be of a specific length type, such as int8/16/32/64, uint8/16/32/64, or float32/64. If the second variable address given corresponds to the int/uint variable type, which has an indeterminate length, parsing will fail.

Furthermore, the gbinary package also provides a series of gbinary.DecodeTo* methods for converting binary to specific data types. Importantly, the gbinary.DecodeToInt/gbinary.DecodeToUint methods automatically recognize and parse the binary length, supporting binary parameter lengths in the range of 1-8.