mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-04 16:22:41 +08:00
27 lines
505 B
Go
27 lines
505 B
Go
package util
|
|
|
|
import "constraints"
|
|
|
|
func PutBE[T constraints.Integer](b []byte, num T) []byte {
|
|
for i, n := 0, len(b); i < n; i++ {
|
|
b[i] = byte(num >> ((n - i - 1) << 3))
|
|
}
|
|
return b
|
|
}
|
|
|
|
func ReadBE[T constraints.Integer](b []byte) (num T) {
|
|
num = 0
|
|
for i, n := 0, len(b); i < n; i++ {
|
|
num += T(b[i]) << ((n - i - 1) << 3)
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetBE[T constraints.Integer](b []byte, num *T) T {
|
|
*num = 0
|
|
for i, n := 0, len(b); i < n; i++ {
|
|
*num += T(b[i]) << ((n - i - 1) << 3)
|
|
}
|
|
return *num
|
|
}
|