util: add Uint256DecodeStringBE()
This commit is contained in:
parent
72fe884faa
commit
aa20a95181
2 changed files with 25 additions and 0 deletions
|
@ -28,6 +28,21 @@ func Uint256DecodeStringLE(s string) (u Uint256, err error) {
|
||||||
return Uint256DecodeBytesLE(b)
|
return Uint256DecodeBytesLE(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Uint256DecodeStringBE attempts to decode the given string (in BE representation)
|
||||||
|
// into an Uint256.
|
||||||
|
func Uint256DecodeStringBE(s string) (u Uint256, err error) {
|
||||||
|
if len(s) != Uint256Size*2 {
|
||||||
|
return u, fmt.Errorf("expected string size of %d got %d", Uint256Size*2, len(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := hex.DecodeString(s)
|
||||||
|
if err != nil {
|
||||||
|
return u, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return Uint256DecodeBytesBE(b)
|
||||||
|
}
|
||||||
|
|
||||||
// Uint256DecodeBytesBE attempts to decode the given string (in BE representation) into an Uint256.
|
// Uint256DecodeBytesBE attempts to decode the given string (in BE representation) into an Uint256.
|
||||||
func Uint256DecodeBytesBE(b []byte) (u Uint256, err error) {
|
func Uint256DecodeBytesBE(b []byte) (u Uint256, err error) {
|
||||||
if len(b) != Uint256Size {
|
if len(b) != Uint256Size {
|
||||||
|
|
|
@ -37,6 +37,10 @@ func TestUint256DecodeString(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, hexStr, val.StringLE())
|
assert.Equal(t, hexStr, val.StringLE())
|
||||||
|
|
||||||
|
valBE, err := Uint256DecodeStringBE(hexStr)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, val, valBE.Reverse())
|
||||||
|
|
||||||
bs, err := hex.DecodeString(hexStr)
|
bs, err := hex.DecodeString(hexStr)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
@ -48,9 +52,15 @@ func TestUint256DecodeString(t *testing.T) {
|
||||||
_, err = Uint256DecodeStringLE(hexStr[1:])
|
_, err = Uint256DecodeStringLE(hexStr[1:])
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, err = Uint256DecodeStringBE(hexStr[1:])
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
hexStr = "zzz7308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
hexStr = "zzz7308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
||||||
_, err = Uint256DecodeStringLE(hexStr)
|
_, err = Uint256DecodeStringLE(hexStr)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, err = Uint256DecodeStringBE(hexStr)
|
||||||
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUint256DecodeBytes(t *testing.T) {
|
func TestUint256DecodeBytes(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue