mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-26 09:43:49 +00:00
util: add Uint160DecodeBytesLE()
This commit is contained in:
parent
09b295d727
commit
07e832f046
2 changed files with 21 additions and 0 deletions
|
@ -35,6 +35,20 @@ func Uint160DecodeBytesBE(b []byte) (u Uint160, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// Uint160DecodeBytesLE attempts to decode the given bytes in little-endian
|
||||
// into an Uint160.
|
||||
func Uint160DecodeBytesLE(b []byte) (u Uint160, err error) {
|
||||
if len(b) != Uint160Size {
|
||||
return u, fmt.Errorf("expected byte size of %d got %d", Uint160Size, len(b))
|
||||
}
|
||||
|
||||
for i := range b {
|
||||
u[Uint160Size-i-1] = b[i]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// BytesBE returns a big-endian byte representation of u.
|
||||
func (u Uint160) BytesBE() []byte {
|
||||
return u[:]
|
||||
|
|
|
@ -52,6 +52,13 @@ func TestUint160DecodeBytes(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.Equal(t, hexStr, val.String())
|
||||
|
||||
valLE, err := Uint160DecodeBytesLE(b)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, val, valLE.Reverse())
|
||||
|
||||
_, err = Uint160DecodeBytesLE(b[1:])
|
||||
assert.Error(t, err)
|
||||
|
||||
_, err = Uint160DecodeBytesBE(b[1:])
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue