2024-01-03 09:56:10 +00:00
|
|
|
package utils
|
|
|
|
|
2024-01-17 15:10:53 +00:00
|
|
|
func EncodeEndian(value uint64, length int) []byte {
|
2024-01-24 15:58:05 +00:00
|
|
|
res := make([]byte, length)
|
|
|
|
|
2024-01-24 16:51:10 +00:00
|
|
|
for i := 0; i < length; i++ {
|
2024-01-24 15:58:05 +00:00
|
|
|
res[i] = byte(value & 0xff)
|
|
|
|
value = value >> 8
|
|
|
|
}
|
|
|
|
return res
|
2024-01-03 09:56:10 +00:00
|
|
|
}
|
2024-01-24 15:58:05 +00:00
|
|
|
func DecodeEndian(bytes []byte) uint64 {
|
2024-01-24 16:23:18 +00:00
|
|
|
var total uint64 = 0
|
|
|
|
var multiplier uint64 = 1
|
2024-01-03 09:56:10 +00:00
|
|
|
|
2024-01-24 15:58:05 +00:00
|
|
|
for _, b := range bytes {
|
2024-01-24 16:23:18 +00:00
|
|
|
total += uint64(b) * multiplier
|
|
|
|
multiplier *= 256
|
2024-01-24 15:58:05 +00:00
|
|
|
}
|
2024-01-03 12:17:37 +00:00
|
|
|
|
2024-01-24 15:58:05 +00:00
|
|
|
return total
|
2024-01-03 09:56:10 +00:00
|
|
|
}
|