You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
package util
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
"math"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Float32ToUint16(order binary.ByteOrder, val float32) []uint16 {
|
|
|
|
|
bs := Float32ToBytes(order, val)
|
|
|
|
|
return BytesToUint16(order, bs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Float32ToBytes(order binary.ByteOrder, val float32) []byte {
|
|
|
|
|
bits := math.Float32bits(val)
|
|
|
|
|
bs := make([]byte, 4)
|
|
|
|
|
order.PutUint32(bs, bits)
|
|
|
|
|
return bs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BytesToUint16 转换byte数组到uint16数组,自定义端序
|
|
|
|
|
func BytesToUint16(byteOrder binary.ByteOrder, bytes []byte) []uint16 {
|
|
|
|
|
values := make([]uint16, len(bytes)/2)
|
|
|
|
|
for i := range values {
|
|
|
|
|
values[i] = byteOrder.Uint16(bytes[i*2 : (i+1)*2])
|
|
|
|
|
}
|
|
|
|
|
return values
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Uint16ToBytes 转换uint16数组到byte数组,大端序
|
|
|
|
|
func Uint16ToBytes(byteOrder binary.ByteOrder, values []uint16) []byte {
|
|
|
|
|
bytes := make([]byte, len(values)*2)
|
|
|
|
|
|
|
|
|
|
for i, value := range values {
|
|
|
|
|
byteOrder.PutUint16(bytes[i*2:(i+1)*2], value)
|
|
|
|
|
}
|
|
|
|
|
return bytes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BitAtPosition 获取某字节指定 位 的值
|
|
|
|
|
func BitAtPosition(value uint8, pos uint) uint8 {
|
|
|
|
|
return (value >> pos) & 0x01
|
|
|
|
|
}
|