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 | """ |
35 | 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:
39 | """ |
---|---|
40 | Remove 0x prefix from a hex string if present. This function returns the |
41 | passed hex string if it isn't prefixed with 0x. |
42 |
|
43 | Parameters |
44 | ---------- |
45 | hex_string : |
46 | The hexadecimal string whose prefix is to be removed. |
47 |
|
48 | Returns |
49 | ------- |
50 | modified_hex_string : `str` |
51 | The hexadecimal string with the 0x prefix removed if present. |
52 |
|
53 | """ |
54 | if has_hex_prefix(hex_string): |
55 | return hex_string[len("0x") :] |
56 | |
57 | 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:
61 | """ |
---|---|
62 | Convert hex string to bytes. |
63 |
|
64 | Parameters |
65 | ---------- |
66 | hex_string : |
67 | The hexadecimal string to be converted to bytes. |
68 |
|
69 | Returns |
70 | ------- |
71 | byte_stream : `bytes` |
72 | Byte stream corresponding to the given hexadecimal string. |
73 |
|
74 | """ |
75 | 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:
79 | """ |
---|---|
80 | Convert hex string to 8 bytes. |
81 |
|
82 | Parameters |
83 | ---------- |
84 | hex_string : |
85 | The hexadecimal string to be converted to 8 bytes. |
86 |
|
87 | Returns |
88 | ------- |
89 | 8_byte_stream : `Bytes8` |
90 | 8-byte stream corresponding to the given hexadecimal string. |
91 |
|
92 | """ |
93 | return Bytes8(Bytes.fromhex(remove_hex_prefix(hex_string).rjust(16, "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:
97 | """ |
---|---|
98 | Convert hex string to 32 bytes. |
99 |
|
100 | Parameters |
101 | ---------- |
102 | hex_string : |
103 | The hexadecimal string to be converted to 32 bytes. |
104 |
|
105 | Returns |
106 | ------- |
107 | 32_byte_stream : `Bytes32` |
108 | 32-byte stream corresponding to the given hexadecimal string. |
109 |
|
110 | """ |
111 | 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:
115 | """ |
---|---|
116 | Convert hex string to 256 bytes. |
117 |
|
118 | Parameters |
119 | ---------- |
120 | hex_string : |
121 | The hexadecimal string to be converted to 256 bytes. |
122 |
|
123 | Returns |
124 | ------- |
125 | 256_byte_stream : `Bytes256` |
126 | 256-byte stream corresponding to the given hexadecimal string. |
127 |
|
128 | """ |
129 | return Bytes256( |
130 | Bytes.fromhex(remove_hex_prefix(hex_string).rjust(512, "0")) |
131 | ) |
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:
135 | """ |
---|---|
136 | Convert hex string to hash32 (32 bytes). |
137 |
|
138 | Parameters |
139 | ---------- |
140 | hex_string : |
141 | The hexadecimal string to be converted to hash32. |
142 |
|
143 | Returns |
144 | ------- |
145 | hash : `Hash32` |
146 | 32-byte stream obtained from the given hexadecimal string. |
147 |
|
148 | """ |
149 | 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:
153 | """ |
---|---|
154 | Convert hex string to Uint. |
155 |
|
156 | Parameters |
157 | ---------- |
158 | hex_string : |
159 | The hexadecimal string to be converted to Uint. |
160 |
|
161 | Returns |
162 | ------- |
163 | converted : `Uint` |
164 | The unsigned integer obtained from the given hexadecimal string. |
165 |
|
166 | """ |
167 | return Uint(int(remove_hex_prefix(hex_string), 16)) |
hex_to_u8
Convert hex string to U8.
Parameters
hex_string : The hexadecimal string to be converted to U8.
Returns
converted : U8
The U8 integer obtained from the given hexadecimal string.
def hex_to_u8(hex_string: str) -> U8:
171 | """ |
---|---|
172 | Convert hex string to U8. |
173 |
|
174 | Parameters |
175 | ---------- |
176 | hex_string : |
177 | The hexadecimal string to be converted to U8. |
178 |
|
179 | Returns |
180 | ------- |
181 | converted : `U8` |
182 | The U8 integer obtained from the given hexadecimal string. |
183 |
|
184 | """ |
185 | return U8(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:
189 | """ |
---|---|
190 | Convert hex string to U64. |
191 |
|
192 | Parameters |
193 | ---------- |
194 | hex_string : |
195 | The hexadecimal string to be converted to U256. |
196 |
|
197 | Returns |
198 | ------- |
199 | converted : `U64` |
200 | The U64 integer obtained from the given hexadecimal string. |
201 |
|
202 | """ |
203 | 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:
207 | """ |
---|---|
208 | Convert hex string to U256. |
209 |
|
210 | Parameters |
211 | ---------- |
212 | hex_string : |
213 | The hexadecimal string to be converted to U256. |
214 |
|
215 | Returns |
216 | ------- |
217 | converted : `U256` |
218 | The U256 integer obtained from the given hexadecimal string. |
219 |
|
220 | """ |
221 | return U256(int(remove_hex_prefix(hex_string), 16)) |