View Source Signet.Hex (Signet v1.3.8)
Helper module for parsing and encoding hex values.
If you use Signet.Hex
, then you can use the ~h
sigil for compile-time
hex-to-binary compilation.
Summary
Functions
Parses an Ethereum 20-bytes hex string.
Parses a hex string, but returns :error
instead
of raising if hex is invalid.
Parses a hex string and raises if invalid.
Parses hex value as a big-endian integer.
Parses hex value as a big-endian integer. Raises if invalid.
Parses hex is value is not nil, otherwise returns nil
.
Parses an Ethereum x-bytes hex string.
Parses an Ethereum 32-bytes hex string.
Encodes a binary as a checksummed Ethereum address.
Encodes hex, in CAPITALS.
Encodes a given value as a lowercase hex string, starting with 0x
.
If input is a tuple {:ok, x}
then returns a tuple {:ok, hex}
where hex = encode(x)
. Otherwise, returns its input unchanged.
Encodes hex, striping any leading zeros.
Alias for decode_hex
.
Alias for decode_hex!
.
Similar non-sigil compile-time hex parser.
If input is non-nil
, returns input encoded as a hex string. Otherwise,
returns nil
.
Handles the sigil ~h
for list of words.
Alias for encode_address
.
Alias for encode_hex
.
Types
@type t() :: binary()
Functions
Parses an Ethereum 20-bytes hex string.
Identical to decode_hex!/1
except fails if
string is not exactly 20-bytes.
Examples
iex> Signet.Hex.decode_address!("0x0000000000000000000000000000000000000001") <<1::160>>
iex> Signet.Hex.decode_address!("0xaabb") ** (Signet.Hex.HexError) invalid hex address: "0xaabb"
Parses a hex string, but returns :error
instead
of raising if hex is invalid.
Examples
iex> Signet.Hex.decode_hex("0xaabb")
iex> Signet.Hex.decode_hex("aabb")
iex> Signet.Hex.decode_hex("0xgggg") :invalid_hex
Parses a hex string and raises if invalid.
Examples
iex> Signet.Hex.decode_hex!("aabb") <<170, 187>>
iex> Signet.Hex.decode_hex!("0xggaabb") ** (Signet.Hex.HexError) invalid hex: "0xggaabb"
Parses hex value as a big-endian integer.
Examples
iex> Signet.Hex.decode_hex_number("0xaabb")
iex> Signet.Hex.decode_hex_number("0xgggg") :invalid_hex
Parses hex value as a big-endian integer. Raises if invalid.
Examples
iex> Signet.Hex.decode_hex_number!("0xaabb") 0xaabb
iex> Signet.Hex.decode_hex_number!("0xgggg") ** (Signet.Hex.HexError) invalid hex number: "0xgggg"
Parses hex is value is not nil, otherwise returns nil
.
Examples
iex> Signet.Hex.decode_maybe_hex!("0xaabb") <<170, 187>>
iex> Signet.Hex.decode_maybe_hex!(nil) nil
Parses an Ethereum x-bytes hex string.
Identical to decode_hex!/1
except fails if
string is not exactly x-bytes.
Examples
iex> Signet.Hex.decode_sized!("0x001122", 3) <<0x00, 0x11, 0x22>>
iex> Signet.Hex.decode_sized!("0xaabb", 3) ** (Signet.Hex.HexError) invalid 3-byte sized hex: "0xaabb"
Parses an Ethereum 32-bytes hex string.
Identical to decode_hex!/1
except fails if
string is not exactly 32-bytes.
Examples
iex> Signet.Hex.decode_word!("0x0000000000000000000000000000000000000000000000000000000000000001") <<1::256>>
iex> Signet.Hex.decode_word!("0xaabb") ** (Signet.Hex.HexError) invalid hex word: "0xaabb"
Encodes a binary as a checksummed Ethereum address.
Examples
iex> Signet.Hex.encode_address(<<0xaa, 0xbb, 0xcc, 0::136>>) "0xaABbcC0000000000000000000000000000000000"
iex> Signet.Hex.encode_address(<<55>>)
** (Signet.Hex.HexError) Expected 20-byte address for in Signet.Hex.encode_address/1
Encodes hex, in CAPITALS.
Examples
iex> Signet.Hex.encode_big_hex(<<0xcc, 0xdd>>) "0xCCDD"
Encodes a given value as a lowercase hex string, starting with 0x
.
Examples
iex> Signet.Hex.encode_hex(<<0xaa, 0xbb>>) "0xaabb"
If input is a tuple {:ok, x}
then returns a tuple {:ok, hex}
where hex = encode(x)
. Otherwise, returns its input unchanged.
Examples
iex> Signet.Hex.encode_hex_result({:ok, <<0xaa, 0xbb>>})
iex> Signet.Hex.encode_hex_result({:error, 55})
Encodes hex, striping any leading zeros.
Examples
iex> Signet.Hex.encode_short_hex(<<0xc>>) "0xC"
iex> Signet.Hex.encode_short_hex(12) "0xC"
iex> Signet.Hex.encode_short_hex(<<0x0>>) "0x0"
Alias for decode_hex
.
Examples
iex> Signet.Hex.from_hex("0xaabb")
Alias for decode_hex!
.
Examples
iex> Signet.Hex.from_hex!("0xaabb") <<0xaa, 0xbb>>
Similar non-sigil compile-time hex parser.
Examples
iex> use Signet.Hex
iex> hex!("0x22")
<<0x22>>
iex> use Signet.Hex
iex> hex!("0x2244")
<<0x22, 0x44>>
If input is non-nil
, returns input encoded as a hex string. Otherwise,
returns nil
.
Examples
iex> Signet.Hex.maybe_encode_hex(<<0xaa, 0xbb>>) "0xaabb"
iex> Signet.Hex.maybe_encode_hex(nil) nil
Handles the sigil ~h
for list of words.
Parses a hex string at compile-time.
Examples
iex> use Signet.Hex
iex> ~h[0x22]
<<0x22>>
iex> use Signet.Hex
iex> ~h[0x2244]
<<0x22, 0x44>>
Alias for encode_address
.
Examples
iex> Signet.Hex.to_address(<<0xaa, 0xbb, 0xcc, 0::136>>) "0xaABbcC0000000000000000000000000000000000"
Alias for encode_hex
.
Examples
iex> Signet.Hex.to_hex(<<0xaa, 0xbb>>) "0xaabb"