forked from uschen/promtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes_utils.go
52 lines (45 loc) · 1.04 KB
/
bytes_utils.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
51
52
package promtable
import (
"encoding/binary"
"math"
)
// Float64ToBytes -
func Float64ToBytes(val float64) []byte {
typeBuf := make([]byte, 8)
binary.BigEndian.PutUint64(typeBuf[:], math.Float64bits(val))
return typeBuf
}
// Float64FromBytes -
func Float64FromBytes(val []byte) float64 {
if len(val) == 0 {
return 0
}
return math.Float64frombits(binary.BigEndian.Uint64(val))
}
// Int32ToBytes -
func Int32ToBytes(val int32) []byte {
typeBuf := make([]byte, 4)
binary.BigEndian.PutUint32(typeBuf, uint32(val))
return typeBuf
}
// Int32FromBytes converts BigEdian bytes encoding
// to int32. bytes can be either 8 bytes or 10 bytes.
func Int32FromBytes(val []byte) int32 {
if len(val) == 0 {
return 0
}
return int32(binary.BigEndian.Uint32(val))
}
// Int64ToBytes -
func Int64ToBytes(val int64) []byte {
typeBuf := make([]byte, 8)
binary.BigEndian.PutUint64(typeBuf, uint64(val))
return typeBuf
}
// Int64FromBytes -
func Int64FromBytes(val []byte) int64 {
if len(val) == 0 {
return 0
}
return int64(binary.BigEndian.Uint64(val))
}