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.
naza/pkg/bele/bele.go

67 lines
1.3 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Package bele 提供了大小端的转换操作
//
// be是big endian的缩写即大端
// le是little endian的缩写即小端
//
// assume local is `le`
//
package bele
import (
"encoding/binary"
"io"
"math"
)
func BEUint16(p []byte) uint16 {
return binary.BigEndian.Uint16(p)
}
func BEUint24(p []byte) (ret uint32) {
ret = 0
ret |= uint32(p[0]) << 16
ret |= uint32(p[1]) << 8
ret |= uint32(p[2])
return
}
func BEUint32(p []byte) (ret uint32) {
return binary.BigEndian.Uint32(p)
}
func BEFloat64(p []byte) (ret float64) {
a := binary.BigEndian.Uint64(p)
return math.Float64frombits(a)
}
func LEUint32(p []byte) (ret uint32) {
return binary.LittleEndian.Uint32(p)
}
func BEPutUint24(out []byte, in uint32) {
out[0] = byte(in >> 16)
out[1] = byte(in >> 8)
out[2] = byte(in)
}
func BEPutUint32(out []byte, in uint32) {
binary.BigEndian.PutUint32(out, in)
}
func LEPutUint32(out []byte, in uint32) {
binary.LittleEndian.PutUint32(out, in)
}
func WriteBEUint24(writer io.Writer, in uint32) error {
_, err := writer.Write([]byte{uint8(in >> 16), uint8(in >> 8), uint8(in & 0xFF)})
return err
}
func WriteBE(writer io.Writer, in interface{}) error {
return binary.Write(writer, binary.BigEndian, in)
}
func WriteLE(writer io.Writer, in interface{}) error {
return binary.Write(writer, binary.LittleEndian, in)
}