-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_byte.go
79 lines (73 loc) · 1.67 KB
/
convert_to_byte.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package u
import (
"bytes"
"encoding/binary"
"math"
)
// ToBytes to []byte
func ToBytes(val interface{}) []byte {
switch v := val.(type) {
case []byte:
return v
case string:
return []byte(val.(string))
case int:
return int64ToBytes(int64(val.(int)))
case int8:
return int64ToBytes(int64(val.(int8)))
case int16:
return int64ToBytes(int64(val.(int16)))
case int32:
return int64ToBytes(int64(val.(int32)))
case int64:
return int64ToBytes(val.(int64))
case uint:
return uint64ToBytes(uint64(val.(uint)))
case uint8:
return uint64ToBytes(uint64(val.(uint8)))
case uint16:
return uint64ToBytes(uint64(val.(uint16)))
case uint32:
return uint64ToBytes(uint64(val.(uint32)))
case uint64:
return uint64ToBytes(val.(uint64))
case float32:
return float32ToBytes(val.(float32))
case float64:
return float64ToBytes(val.(float64))
default:
return []byte{}
}
}
// int64ToBytes int64 to []byte
func int64ToBytes(val int64) []byte {
buffer := bytes.NewBuffer(nil)
err := binary.Write(buffer, binary.BigEndian, val)
if err != nil {
return []byte{}
}
return buffer.Bytes()
}
// uint64ToBytes uint64 to []byte
func uint64ToBytes(val uint64) []byte {
buffer := bytes.NewBuffer(nil)
err := binary.Write(buffer, binary.BigEndian, val)
if err != nil {
return []byte{}
}
return buffer.Bytes()
}
// float32ToBytes float32 to []byte
func float32ToBytes(val float32) []byte {
bits := math.Float32bits(val)
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, bits)
return b
}
// float64ToBytes float64 to []byte
func float64ToBytes(val float64) []byte {
bits := math.Float64bits(val)
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, bits)
return b
}