MCPcopy
hub / github.com/gogf/gf / BeEncode

Function BeEncode

encoding/gbinary/gbinary_be.go:26–72  ·  view source on GitHub ↗

BeEncode encodes one or multiple `values` into bytes using BigEndian. It uses type asserting checking the type of each value of `values` and internally calls corresponding converting function do the bytes converting. It supports common variable type asserting, and finally it uses fmt.Sprintf conver

(values ...any)

Source from the content-addressed store, hash-verified

24// It supports common variable type asserting, and finally it uses fmt.Sprintf converting
25// value to string and then to bytes.
26func BeEncode(values ...any) []byte {
27 buf := new(bytes.Buffer)
28 for i := 0; i < len(values); i++ {
29 if values[i] == nil {
30 return buf.Bytes()
31 }
32
33 switch value := values[i].(type) {
34 case int:
35 buf.Write(BeEncodeInt(value))
36 case int8:
37 buf.Write(BeEncodeInt8(value))
38 case int16:
39 buf.Write(BeEncodeInt16(value))
40 case int32:
41 buf.Write(BeEncodeInt32(value))
42 case int64:
43 buf.Write(BeEncodeInt64(value))
44 case uint:
45 buf.Write(BeEncodeUint(value))
46 case uint8:
47 buf.Write(BeEncodeUint8(value))
48 case uint16:
49 buf.Write(BeEncodeUint16(value))
50 case uint32:
51 buf.Write(BeEncodeUint32(value))
52 case uint64:
53 buf.Write(BeEncodeUint64(value))
54 case bool:
55 buf.Write(BeEncodeBool(value))
56 case string:
57 buf.Write(BeEncodeString(value))
58 case []byte:
59 buf.Write(value)
60 case float32:
61 buf.Write(BeEncodeFloat32(value))
62 case float64:
63 buf.Write(BeEncodeFloat64(value))
64 default:
65 if err := binary.Write(buf, binary.BigEndian, value); err != nil {
66 intlog.Errorf(context.TODO(), `%+v`, err)
67 buf.Write(BeEncodeString(fmt.Sprintf("%v", value)))
68 }
69 }
70 }
71 return buf.Bytes()
72}
73
74func BeEncodeByLength(length int, values ...any) []byte {
75 b := BeEncode(values...)

Callers 3

Test_BeEncodeAndBeDecodeFunction · 0.92
Test_BeEncodeStructFunction · 0.92
BeEncodeByLengthFunction · 0.85

Calls 15

ErrorfFunction · 0.92
BeEncodeIntFunction · 0.85
BeEncodeInt8Function · 0.85
BeEncodeInt16Function · 0.85
BeEncodeInt32Function · 0.85
BeEncodeInt64Function · 0.85
BeEncodeUintFunction · 0.85
BeEncodeUint8Function · 0.85
BeEncodeUint16Function · 0.85
BeEncodeUint32Function · 0.85
BeEncodeUint64Function · 0.85
BeEncodeBoolFunction · 0.85

Tested by 2

Test_BeEncodeAndBeDecodeFunction · 0.74
Test_BeEncodeStructFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…