1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00

bytesconv

This commit is contained in:
2022-03-29 10:24:43 +08:00
parent 42f85a2518
commit bd4a7aed01
6 changed files with 86 additions and 91 deletions

31
bytesconv/uint64.go Normal file
View File

@ -0,0 +1,31 @@
package bytesconv
import "fmt"
type endian int
const (
BigEndian endian = iota + 1
LittleEndian
)
func (e endian) BytesToUInt64(data []byte) (uint64, error) {
if len(data) > 8 {
return 0, fmt.Errorf("bytes to uint64, bytes length is invaild")
}
var ret uint64
var len int = len(data)
if e == BigEndian {
for i := 0; i < len; i++ {
ret = ret | (uint64(data[len-1-i]) << (i * 8))
}
} else {
for i := 0; i < len; i++ {
ret = ret | (uint64(data[i]) << (i * 8))
}
}
return ret, nil
}