module Rlp::Util
Overview
Exposes a set of utilities to ease the handling of different data types. It comes in handy when building protocols further decoding RLP byte-streams.
decoded = Rlp.decode Bytes[197, 42, 131, 101, 116, 104]
pp decoded
# => [Bytes[42], Bytes[101, 116, 104]]
protocol = [] of String | Int32 | BigInt
protocol << Rlp::Util.bin_to_int decoded[0]
protocol << Rlp::Util.bin_to_str decoded[1]
pp protocol
# => [42, "eth"]
Defined in:
util.crClass Method Summary
-
.bin_to_hex(b : Bytes)
Converts binary
Bytesto a hex-encodedString. -
.bin_to_int(b : Bytes)
Converts binary
Bytesto aBigInt. -
.bin_to_int(a)
Overloads
.bin_to_intwith arbitrary data types and raises if input data is not binary. -
.bin_to_str(b : Bytes)
Converts binary
Bytesto aStringliteral. -
.bin_to_str(a)
Overloads
.bin_to_strwith arbitrary data types and raises if input data is not binary. -
.binary_add(a : Bytes, b : Bytes)
Concatenates two
Bytesslices ofUInt8. -
.hex_to_bin(h : String)
Converts hex-encoded
Strings to binaryBytesdata. -
.hex_to_int(h : String)
Converts hex-encoded
Strings toBigInts. -
.hex_to_str(h : String)
Converts hex-encoded
Strings toStringliterals. -
.int_to_bin(i : Int)
Converts integers to binary
Bytes. -
.int_to_hex(i : Int)
Converts integers to hex-encoded
Strings. -
.str_to_bin(s : String)
Converts
Stringliterals to binaryBytesdata. -
.str_to_hex(s : String)
Converts
Stringliterals to hex-encodedStrings.
Class Method Detail
Converts binary Bytes to a hex-encoded String.
Parameters:
b(Bytes): the binaryBytesdata to convert.
Rlp::Util.bin_to_hex Bytes[4, 200, 29]
# => "04c81d"
Converts binary Bytes to a BigInt.
Parameters:
b(Bytes): the binaryBytesdata to convert.
Rlp::Util.bin_to_int Bytes[15, 66, 64]
# => 1000000
Overloads .bin_to_int with arbitrary data types and raises if
input data is not binary.
NOTE Raises in any case if a actually contains non-binary or nested data.
Shouldn't be used if decoded Rlp data could contain nested data structures.
Converts binary Bytes to a String literal.
Parameters:
b(Bytes): the binaryBytesdata to convert.
Rlp::Util.bin_to_str Bytes[97, 98, 99]
# => "abc"
Overloads .bin_to_str with arbitrary data types and raises if
input data is not binary.
NOTE Raises in any case if a actually contains non-binary or nested data.
Shouldn't be used if decoded Rlp data could contain nested data structures.
Concatenates two Bytes slices of UInt8.
a = Bytes[131]
b = Bytes[97, 98, 99]
Rlp::Util.binary_add a, b
# => Bytes[131, 97, 98, 99]
Converts hex-encoded Strings to binary Bytes data.
Parameters:
h(String): the hex-encodedStringto convert.
Rlp::Util.hex_to_bin "04c81d"
# => Bytes[4, 200, 29]
Converts hex-encoded Strings to BigInts.
Parameters:
h(String): the hex-encodedStringto convert.
Rlp::Util.hex_to_int "04c81d"
# => 313373
Converts hex-encoded Strings to String literals.
Parameters:
h(String): the hex-encodedStringto convert.
Rlp::Util.hex_to_str "646f67"
# => "dog"
Converts integers to binary Bytes.
Parameters:
i(Int): the integer to convert.
Rlp::Util.int_to_bin 1_000_000
# => Bytes[15, 66, 64]
Converts integers to hex-encoded Strings.
Parameters:
i(Int): the integer to convert.
Rlp::Util.int_to_hex 313_373
# => "04c81d"
Converts String literals to binary Bytes data.
Parameters:
s(String): theStringliteral to convert.
Rlp::Util.str_to_bin "abc"
# => Bytes[97, 98, 99]
Converts String literals to hex-encoded Strings.
Parameters:
s(String): theStringliteral to convert.
Rlp::Util.str_to_hex "dog"
# => "646f67"