Utility Functions For Hexadecimal Strings
Table of Contents
Introduction
Hexadecimal strings specific utility functions used in this specification.
Module Contents
Functions
Check if a hex string starts with hex prefix (0x). |
|
Remove 0x prefix from a hex string if present. This function returns the |
|
Convert hex string to bytes. |
|
Convert hex string to 8 bytes. |
|
Convert hex string to 32 bytes. |
|
Convert hex string to 256 bytes. |
|
Convert hex string to hash32 (32 bytes). |
|
Convert hex string to Uint. |
|
Convert hex string to Uint64. |
|
Convert hex string to U256. |
Module Details
has_hex_prefix
- has_hex_prefix(hex_string: str) → bool
Check if a hex string starts with hex prefix (0x).
- Parameters
hex_string – The hexadecimal string to be checked for presence of prefix.
- Returns
has_prefix – Boolean indicating whether the hex string has 0x prefix.
- Return type
bool
def has_hex_prefix(hex_string: str) -> bool:
return hex_string.startswith("0x")
remove_hex_prefix
- remove_hex_prefix(hex_string: str) → str
Remove 0x prefix from a hex string if present. This function returns the passed hex string if it isn’t prefixed with 0x.
- Parameters
hex_string – The hexadecimal string whose prefix is to be removed.
- Returns
modified_hex_string – The hexadecimal string with the 0x prefix removed if present.
- Return type
str
def remove_hex_prefix(hex_string: str) -> str:
if has_hex_prefix(hex_string):
return hex_string[len("0x") :]
return hex_string
hex_to_bytes
- hex_to_bytes(hex_string: str) → ethereum.base_types.Bytes
Convert hex string to bytes.
- Parameters
hex_string – The hexadecimal string to be converted to bytes.
- Returns
byte_stream – Byte stream corresponding to the given hexadecimal string.
- Return type
bytes
def hex_to_bytes(hex_string: str) -> Bytes:
return bytes.fromhex(remove_hex_prefix(hex_string))
hex_to_bytes8
- hex_to_bytes8(hex_string: str) → ethereum.base_types.Bytes8
Convert hex string to 8 bytes.
- Parameters
hex_string – The hexadecimal string to be converted to 8 bytes.
- Returns
8_byte_stream – 8-byte stream corresponding to the given hexadecimal string.
- Return type
bytes
def hex_to_bytes8(hex_string: str) -> Bytes8:
return Bytes8(bytes.fromhex(remove_hex_prefix(hex_string).rjust(16, "0")))
hex_to_bytes32
- hex_to_bytes32(hex_string: str) → ethereum.base_types.Bytes32
Convert hex string to 32 bytes.
- Parameters
hex_string – The hexadecimal string to be converted to 32 bytes.
- Returns
32_byte_stream – 32-byte stream corresponding to the given hexadecimal string.
- Return type
bytes
def hex_to_bytes32(hex_string: str) -> Bytes32:
return Bytes32(bytes.fromhex(remove_hex_prefix(hex_string).rjust(64, "0")))
hex_to_bytes256
- hex_to_bytes256(hex_string: str) → ethereum.base_types.Bytes256
Convert hex string to 256 bytes.
- Parameters
hex_string – The hexadecimal string to be converted to 256 bytes.
- Returns
256_byte_stream – 256-byte stream corresponding to the given hexadecimal string.
- Return type
bytes
def hex_to_bytes256(hex_string: str) -> Bytes256:
return Bytes256(
bytes.fromhex(remove_hex_prefix(hex_string).rjust(512, "0"))
)
hex_to_hash
- hex_to_hash(hex_string: str) → ethereum.crypto.hash.Hash32
Convert hex string to hash32 (32 bytes).
- Parameters
hex_string – The hexadecimal string to be converted to hash32.
- Returns
hash – 32-byte stream obtained from the given hexadecimal string.
- Return type
Hash32
def hex_to_hash(hex_string: str) -> Hash32:
return Hash32(bytes.fromhex(remove_hex_prefix(hex_string)))
hex_to_uint
- hex_to_uint(hex_string: str) → ethereum.base_types.Uint
Convert hex string to Uint.
- Parameters
hex_string – The hexadecimal string to be converted to Uint.
- Returns
converted – The unsigned integer obtained from the given hexadecimal string.
- Return type
Uint
def hex_to_uint(hex_string: str) -> Uint:
return Uint(int(remove_hex_prefix(hex_string), 16))
hex_to_u64
- hex_to_u64(hex_string: str) → ethereum.base_types.Uint64
Convert hex string to Uint64.
- Parameters
hex_string – The hexadecimal string to be converted to U256.
- Returns
converted – The Uint64 integer obtained from the given hexadecimal string.
- Return type
Uint64
def hex_to_u64(hex_string: str) -> Uint64:
return Uint64(int(remove_hex_prefix(hex_string), 16))
hex_to_u256
- hex_to_u256(hex_string: str) → ethereum.base_types.U256
Convert hex string to U256.
- Parameters
hex_string – The hexadecimal string to be converted to U256.
- Returns
converted – The U256 integer obtained from the given hexadecimal string.
- Return type
U256
def hex_to_u256(hex_string: str) -> U256:
return U256(int(remove_hex_prefix(hex_string), 16))