/
byte.go
65 lines (53 loc) · 1021 Bytes
/
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
package fun
import (
"encoding/binary"
"io"
"math"
"reflect"
"unsafe"
)
/*
【名称:】[]byte转float32
【参数:】[]byte
【返回:】float32
【备注:】
*/
func BytesToFloat32(bytes []byte) float32 {
bits := binary.LittleEndian.Uint32(bytes)
return math.Float32frombits(bits)
}
/*
【名称:】[]byte转float64
【参数:】[]byte
【返回:】float64
【备注:】
*/
func BytesToFloat64(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
return math.Float64frombits(bits)
}
/*
【名称:】不安全的[]byte转string
【参数:】[]byte
【返回:】string
【备注:】
*/
func UnsafeBytesToString(bytes []byte) string {
if len(bytes) == 0 {
return ""
}
hdr := &reflect.StringHeader{
Data: uintptr(unsafe.Pointer(&bytes[0])),
Len: len(bytes),
}
return *(*string)(unsafe.Pointer(hdr))
}
/*
【名称:】[]byte转io.Reader
【参数:】[]byte
【返回:】io.Reader
【备注:】
*/
func BytesToIoReader(b []byte) io.Reader {
return IoReader(b)
}