-
Notifications
You must be signed in to change notification settings - Fork 6
/
bigEndianConverter.go
42 lines (31 loc) · 1008 Bytes
/
bigEndianConverter.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
39
40
41
42
package uint64ByteSlice
import (
"encoding/binary"
"github.com/multiversx/mx-chain-core-go/data/typeConverters"
)
type bigEndianConverter struct {
}
// NewBigEndianConverter creates a big endian uint64-byte slice converter
func NewBigEndianConverter() *bigEndianConverter {
return &bigEndianConverter{}
}
// ToByteSlice converts uint64 to its byte slice representation
func (*bigEndianConverter) ToByteSlice(value uint64) []byte {
buff := make([]byte, 8)
binary.BigEndian.PutUint64(buff, value)
return buff
}
// ToUint64 converts a byte slice to uint64. Errors if something went wrong
func (*bigEndianConverter) ToUint64(buff []byte) (uint64, error) {
if buff == nil {
return 0, typeConverters.ErrNilByteSlice
}
if len(buff) != 8 {
return 0, typeConverters.ErrByteSliceLenShouldHaveBeen8
}
return binary.BigEndian.Uint64(buff), nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (bec *bigEndianConverter) IsInterfaceNil() bool {
return bec == nil
}