-
Notifications
You must be signed in to change notification settings - Fork 294
/
binaryread.go
92 lines (82 loc) · 1.8 KB
/
binaryread.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
80
81
82
83
84
85
86
87
88
89
90
91
92
package encoding
import (
"io"
"math"
)
//LittleEndian
func BinaryReadINT32(r io.Reader, nums []interface{}) error {
buf := make([]byte, len(nums)*4)
n, err := r.Read(buf)
if err != nil {
return err
}
if len(nums)*4 != n {
return io.ErrUnexpectedEOF
}
for i := 0; i < len(nums); i++ {
nums[i] = int32(uint32(buf[i*4+0]) |
uint32(buf[i*4+1])<<8 |
uint32(buf[i*4+2])<<16 |
uint32(buf[i*4+3])<<24)
}
return nil
}
func BinaryReadINT64(r io.Reader, nums []interface{}) error {
buf := make([]byte, len(nums)*8)
n, err := r.Read(buf)
if err != nil {
return err
}
if len(nums)*8 != n {
return io.ErrUnexpectedEOF
}
for i := 0; i < len(nums); i++ {
nums[i] = int64(uint64(buf[i*8+0]) |
uint64(buf[i*8+1])<<8 |
uint64(buf[i*8+2])<<16 |
uint64(buf[i*8+3])<<24 |
uint64(buf[i*8+4])<<32 |
uint64(buf[i*8+5])<<40 |
uint64(buf[i*8+6])<<48 |
uint64(buf[i*8+7])<<56)
}
return nil
}
func BinaryReadFLOAT32(r io.Reader, nums []interface{}) error {
buf := make([]byte, len(nums)*4)
n, err := r.Read(buf)
if err != nil {
return err
}
if len(nums)*4 != n {
return io.ErrUnexpectedEOF
}
for i := 0; i < len(nums); i++ {
nums[i] = math.Float32frombits(uint32(buf[i*4+0]) |
uint32(buf[i*4+1])<<8 |
uint32(buf[i*4+2])<<16 |
uint32(buf[i*4+3])<<24)
}
return nil
}
func BinaryReadFLOAT64(r io.Reader, nums []interface{}) error {
buf := make([]byte, len(nums)*8)
n, err := r.Read(buf)
if err != nil {
return err
}
if len(nums)*8 != n {
return io.ErrUnexpectedEOF
}
for i := 0; i < len(nums); i++ {
nums[i] = math.Float64frombits(uint64(buf[i*8+0]) |
uint64(buf[i*8+1])<<8 |
uint64(buf[i*8+2])<<16 |
uint64(buf[i*8+3])<<24 |
uint64(buf[i*8+4])<<32 |
uint64(buf[i*8+5])<<40 |
uint64(buf[i*8+6])<<48 |
uint64(buf[i*8+7])<<56)
}
return nil
}