-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
50 lines (42 loc) · 1.11 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// description:
// @author renshiwei
// Date: 2022/11/15 18:32
package util
import (
"bytes"
"encoding/binary"
)
// IntToBytes Integers are converted to bytes.
func IntToBytes(n int) []byte {
x := int32(n)
bytesBuffer := bytes.NewBuffer([]byte{})
_ = binary.Write(bytesBuffer, binary.BigEndian, x)
return bytesBuffer.Bytes()
}
// Int32ToBytes Integers are converted to bytes.
func Int32ToBytes(n int32) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
_ = binary.Write(bytesBuffer, binary.BigEndian, n)
return bytesBuffer.Bytes()
}
// Int64ToBytes Integers are converted to bytes.
func Int64ToBytes(n int64) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
_ = binary.Write(bytesBuffer, binary.BigEndian, n)
return bytesBuffer.Bytes()
}
// BytesToInt Bytes are converted to shapes.
func BytesToInt(b []byte) int {
bytesBuffer := bytes.NewBuffer(b)
var x int32
_ = binary.Read(bytesBuffer, binary.BigEndian, &x)
return int(x)
}
// PadByteTo Make up 0.
func PadByteTo(n int) []byte {
return make([]byte, n)
}
// PadByteTo32 Make up 0 to 32 bytes.
func PadByteTo32(n int) []byte {
return make([]byte, 32-n)
}