Right now in the functional test framework, conversions from hex-string to bytes and vice-versa are done in different ways:
- convert
bytes_obj
to hex-string: binascii.hexlify(bytes_obj), bytes_obj.hex() - convert
hex_str
to bytes object: binascii.unhexlify(hex_str), binascii.a2b_hex(hex_str), bytes.fromhex(hex_str)
For consistency reasons, only the built-in way of using bytes
(class) methods should be used (see also MarcoFalke’s comment #22593 (comment)):
- bytes.fromhex() (https://docs.python.org/3.7/library/stdtypes.html#bytes.fromhex)
- bytes.hex() (https://docs.python.org/3.7/library/stdtypes.html#bytes.hex)
This seems to be the most natural and modern way to do it, without a need to import any libraries, and the other ones using binascii should be replaced by those. As an example, see PR #22593 which replaced a helper hex_str_to_bytes
(which in turn used binascii.unhexlify) with bytes.fromhex()
. For quickly identifying instances the command git grep binascii
can be used, though one would need to check that there is maybe uses of binascii that are still legit.
Useful skills:
- Python 3 basic skills, knowledge of fundamental data types string and bytes
Want to work on this issue?
For guidance on contributing, please read CONTRIBUTING.md before opening your pull request.