blob: 77e87af9bf0d52343da4b50df092d9942e51ba86 (
plain) (
blame)
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
|
package utils
import (
"encoding/binary"
"log"
"math"
"os"
"unsafe"
)
func ReadByteFromFile(file *os.File, size int32) []byte {
tmp := make([]byte, size)
file.Read(tmp)
return tmp
}
func CheckError(e error) {
if e != nil {
log.Panic(e)
}
}
/*
github.com/32bitkid/bitreader
func ReadBitsWithFirstBitCheckFromFile(file *os.File) (byteArr []byte, err error) {
arr := make([]byte, 4)
reader := bitreader.NewReader(file)
n := 0
state, err := reader.Read1()
if err != nil || state == true {
return nil, fmt.Errorf("ERR or VAL in BIT CHECK")
}
n += 1
if n == 0 {
val, err := reader.Read32(32)
if err != nil {
return nil, fmt.Errorf("ERR or VAL in BIT CHECK")
}
binary.LittleEndian.PutUint32(arr, val)
}
return arr, nil
}
*/
func IntFromBytes(byteArr []byte) uint32 {
int := binary.LittleEndian.Uint32(byteArr)
return int
}
func FloatFromBytes(byteArr []byte) float32 {
bits := binary.LittleEndian.Uint32(byteArr)
float := math.Float32frombits(bits)
return float
}
func FloatArrFromBytes(byteArr []byte) []float32 {
if len(byteArr) == 0 {
return nil
}
l := len(byteArr) / 4
ptr := unsafe.Pointer(&byteArr[0])
// It is important to keep in mind that the Go garbage collector
// will not interact with this data, and that if src if freed,
// the behavior of any Go code using the slice is nondeterministic.
// Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l]
}
|