libs5-go/utils/endian.go

21 lines
341 B
Go
Raw Normal View History

2024-01-03 09:56:10 +00:00
package utils
func EncodeEndian(value uint64, length int) []byte {
res := make([]byte, length)
for i := length - 1; i >= 0; i-- {
res[i] = byte(value & 0xff)
value = value >> 8
}
return res
2024-01-03 09:56:10 +00:00
}
func DecodeEndian(bytes []byte) uint64 {
var total uint64
2024-01-03 09:56:10 +00:00
for _, b := range bytes {
total = total*256 + uint64(b)
}
return total
2024-01-03 09:56:10 +00:00
}