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
Bytes
to a hex-encodedString
. -
.bin_to_int(b : Bytes)
Converts binary
Bytes
to aBigInt
. -
.bin_to_int(a)
Overloads
.bin_to_int
with arbitrary data types and raises if input data is not binary. -
.bin_to_str(b : Bytes)
Converts binary
Bytes
to aString
literal. -
.bin_to_str(a)
Overloads
.bin_to_str
with arbitrary data types and raises if input data is not binary. -
.binary_add(a : Bytes, b : Bytes)
Concatenates two
Bytes
slices ofUInt8
. -
.hex_to_bin(h : String)
Converts hex-encoded
String
s to binaryBytes
data. -
.hex_to_int(h : String)
Converts hex-encoded
String
s toBigInt
s. -
.hex_to_str(h : String)
Converts hex-encoded
String
s toString
literals. -
.int_to_bin(i : Int)
Converts integers to binary
Bytes
. -
.int_to_hex(i : Int)
Converts integers to hex-encoded
String
s. -
.str_to_bin(s : String)
Converts
String
literals to binaryBytes
data. -
.str_to_hex(s : String)
Converts
String
literals to hex-encodedString
s.
Class Method Detail
Converts binary Bytes
to a hex-encoded String
.
Parameters:
b
(Bytes
): the binaryBytes
data to convert.
Rlp::Util.bin_to_hex Bytes[4, 200, 29]
# => "04c81d"
Converts binary Bytes
to a BigInt
.
Parameters:
b
(Bytes
): the binaryBytes
data 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 binaryBytes
data 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 String
s to binary Bytes
data.
Parameters:
h
(String
): the hex-encodedString
to convert.
Rlp::Util.hex_to_bin "04c81d"
# => Bytes[4, 200, 29]
Converts hex-encoded String
s to BigInt
s.
Parameters:
h
(String
): the hex-encodedString
to convert.
Rlp::Util.hex_to_int "04c81d"
# => 313373
Converts hex-encoded String
s to String
literals.
Parameters:
h
(String
): the hex-encodedString
to 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 String
s.
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
): theString
literal to convert.
Rlp::Util.str_to_bin "abc"
# => Bytes[97, 98, 99]
Converts String
literals to hex-encoded String
s.
Parameters:
s
(String
): theString
literal to convert.
Rlp::Util.str_to_hex "dog"
# => "646f67"