ethereum.utils.hexadecimal

Utility Functions For Hexadecimal Strings ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Hexadecimal strings specific utility functions used in this specification.

has_hex_prefix

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 : bool Boolean indicating whether the hex string has 0x prefix.

def has_hex_prefix(hex_string: str) -> bool:
21
    """
22
    Check if a hex string starts with hex prefix (0x).
23
24
    Parameters
25
    ----------
26
    hex_string :
27
        The hexadecimal string to be checked for presence of prefix.
28
29
    Returns
30
    -------
31
    has_prefix : `bool`
32
        Boolean indicating whether the hex string has 0x prefix.
33
    """
34
    return hex_string.startswith("0x")

remove_hex_prefix

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 : str The hexadecimal string with the 0x prefix removed if present.

def remove_hex_prefix(hex_string: str) -> str:
38
    """
39
    Remove 0x prefix from a hex string if present. This function returns the
40
    passed hex string if it isn't prefixed with 0x.
41
42
    Parameters
43
    ----------
44
    hex_string :
45
        The hexadecimal string whose prefix is to be removed.
46
47
    Returns
48
    -------
49
    modified_hex_string : `str`
50
        The hexadecimal string with the 0x prefix removed if present.
51
    """
52
    if has_hex_prefix(hex_string):
53
        return hex_string[len("0x") :]
54
55
    return hex_string

hex_to_bytes

Convert hex string to bytes.

Parameters

hex_string : The hexadecimal string to be converted to bytes.

Returns

byte_stream : bytes Byte stream corresponding to the given hexadecimal string.

def hex_to_bytes(hex_string: str) -> Bytes:
59
    """
60
    Convert hex string to bytes.
61
62
    Parameters
63
    ----------
64
    hex_string :
65
        The hexadecimal string to be converted to bytes.
66
67
    Returns
68
    -------
69
    byte_stream : `bytes`
70
        Byte stream corresponding to the given hexadecimal string.
71
    """
72
    return bytes.fromhex(remove_hex_prefix(hex_string))

hex_to_bytes8

Convert hex string to 8 bytes.

Parameters

hex_string : The hexadecimal string to be converted to 8 bytes.

Returns

8_byte_stream : Bytes8 8-byte stream corresponding to the given hexadecimal string.

def hex_to_bytes8(hex_string: str) -> Bytes8:
76
    """
77
    Convert hex string to 8 bytes.
78
79
    Parameters
80
    ----------
81
    hex_string :
82
        The hexadecimal string to be converted to 8 bytes.
83
84
    Returns
85
    -------
86
    8_byte_stream : `Bytes8`
87
        8-byte stream corresponding to the given hexadecimal string.
88
    """
89
    return Bytes8(bytes.fromhex(remove_hex_prefix(hex_string).rjust(16, "0")))

hex_to_bytes20

Convert hex string to 20 bytes.

Parameters

hex_string : The hexadecimal string to be converted to 20 bytes.

Returns

20_byte_stream : Bytes20 20-byte stream corresponding to the given hexadecimal string.

def hex_to_bytes20(hex_string: str) -> Bytes20:
93
    """
94
    Convert hex string to 20 bytes.
95
96
    Parameters
97
    ----------
98
    hex_string :
99
        The hexadecimal string to be converted to 20 bytes.
100
101
    Returns
102
    -------
103
    20_byte_stream : `Bytes20`
104
        20-byte stream corresponding to the given hexadecimal string.
105
    """
106
    return Bytes20(bytes.fromhex(remove_hex_prefix(hex_string).rjust(20, "0")))

hex_to_bytes32

Convert hex string to 32 bytes.

Parameters

hex_string : The hexadecimal string to be converted to 32 bytes.

Returns

32_byte_stream : Bytes32 32-byte stream corresponding to the given hexadecimal string.

def hex_to_bytes32(hex_string: str) -> Bytes32:
110
    """
111
    Convert hex string to 32 bytes.
112
113
    Parameters
114
    ----------
115
    hex_string :
116
        The hexadecimal string to be converted to 32 bytes.
117
118
    Returns
119
    -------
120
    32_byte_stream : `Bytes32`
121
        32-byte stream corresponding to the given hexadecimal string.
122
    """
123
    return Bytes32(bytes.fromhex(remove_hex_prefix(hex_string).rjust(64, "0")))

hex_to_bytes256

Convert hex string to 256 bytes.

Parameters

hex_string : The hexadecimal string to be converted to 256 bytes.

Returns

256_byte_stream : Bytes256 256-byte stream corresponding to the given hexadecimal string.

def hex_to_bytes256(hex_string: str) -> Bytes256:
127
    """
128
    Convert hex string to 256 bytes.
129
130
    Parameters
131
    ----------
132
    hex_string :
133
        The hexadecimal string to be converted to 256 bytes.
134
135
    Returns
136
    -------
137
    256_byte_stream : `Bytes256`
138
        256-byte stream corresponding to the given hexadecimal string.
139
    """
140
    return Bytes256(
141
        bytes.fromhex(remove_hex_prefix(hex_string).rjust(512, "0"))
142
    )

hex_to_hash

Convert hex string to hash32 (32 bytes).

Parameters

hex_string : The hexadecimal string to be converted to hash32.

Returns

hash : Hash32 32-byte stream obtained from the given hexadecimal string.

def hex_to_hash(hex_string: str) -> Hash32:
146
    """
147
    Convert hex string to hash32 (32 bytes).
148
149
    Parameters
150
    ----------
151
    hex_string :
152
        The hexadecimal string to be converted to hash32.
153
154
    Returns
155
    -------
156
    hash : `Hash32`
157
        32-byte stream obtained from the given hexadecimal string.
158
    """
159
    return Hash32(bytes.fromhex(remove_hex_prefix(hex_string)))

hex_to_uint

Convert hex string to Uint.

Parameters

hex_string : The hexadecimal string to be converted to Uint.

Returns

converted : Uint The unsigned integer obtained from the given hexadecimal string.

def hex_to_uint(hex_string: str) -> Uint:
163
    """
164
    Convert hex string to Uint.
165
166
    Parameters
167
    ----------
168
    hex_string :
169
        The hexadecimal string to be converted to Uint.
170
171
    Returns
172
    -------
173
    converted : `Uint`
174
        The unsigned integer obtained from the given hexadecimal string.
175
    """
176
    return Uint(int(remove_hex_prefix(hex_string), 16))

hex_to_u64

Convert hex string to U64.

Parameters

hex_string : The hexadecimal string to be converted to U256.

Returns

converted : U64 The U64 integer obtained from the given hexadecimal string.

def hex_to_u64(hex_string: str) -> U64:
180
    """
181
    Convert hex string to U64.
182
183
    Parameters
184
    ----------
185
    hex_string :
186
        The hexadecimal string to be converted to U256.
187
188
    Returns
189
    -------
190
    converted : `U64`
191
        The U64 integer obtained from the given hexadecimal string.
192
    """
193
    return U64(int(remove_hex_prefix(hex_string), 16))

hex_to_u256

Convert hex string to U256.

Parameters

hex_string : The hexadecimal string to be converted to U256.

Returns

converted : U256 The U256 integer obtained from the given hexadecimal string.

def hex_to_u256(hex_string: str) -> U256:
197
    """
198
    Convert hex string to U256.
199
200
    Parameters
201
    ----------
202
    hex_string :
203
        The hexadecimal string to be converted to U256.
204
205
    Returns
206
    -------
207
    converted : `U256`
208
        The U256 integer obtained from the given hexadecimal string.
209
    """
210
    return U256(int(remove_hex_prefix(hex_string), 16))