-
Notifications
You must be signed in to change notification settings - Fork 319
/
byteutil.go
38 lines (32 loc) · 1.22 KB
/
byteutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package byteutil
import (
"github.com/iotexproject/iotex-core/pkg/enc"
)
// Uint32ToBytes converts a uint32 to 4 bytes with the machine endian
func Uint32ToBytes(value uint32) []byte {
bytes := make([]byte, 4)
enc.MachineEndian.PutUint32(bytes, value)
return bytes
}
// Uint64ToBytes converts a uint64 to 8 bytes with the machine endian
func Uint64ToBytes(value uint64) []byte {
bytes := make([]byte, 8)
enc.MachineEndian.PutUint64(bytes, value)
return bytes
}
// BytesToUint64 converts 8 bytes with the machine endian to uint64
func BytesToUint64(value []byte) uint64 {
return enc.MachineEndian.Uint64(value)
}
// Must is a helper wraps a call to a function returing ([]byte, error) and panics if the error is not nil.
func Must(d []byte, err error) []byte {
if err != nil {
panic(err)
}
return d
}