HexBytes
Python bytes subclass that decodes hex, with a readable console output
Contents
hexbytes package
Quickstart
Install with:
python -m pip install hexbytes
Example HexBytes
usage:
>>> from hexbytes import HexBytes
# convert from bytes to a prettier representation at the console
>>> HexBytes(b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;")
HexBytes('0x03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b')
# HexBytes accepts the hex string representation as well, ignoring case and 0x prefixes
>>> hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B')
>>> hb
HexBytes('0x03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b')
# HexBytes does not override the .hex() or __str__ methods of the parent `bytes` type
>>> hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B')
>>> hb.hex()
'03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b'
>>> print(hb)
b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;"
# Use the `to_0x_hex` method to get a 0x-prefixed hex string
>>> hb.to_0x_hex()
'0x03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b'
# get the first byte:
>>> hb[0]
3
# show how many bytes are in the value
>>> len(hb)
32
# cast back to the basic `bytes` type
>>> bytes(hb)
b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;"
HexBytes
- class hexbytes.main.HexBytes(val: bool | bytearray | bytes | int | str | memoryview)
Bases:
bytes
HexBytes is a very thin wrapper around the python built-in
bytes
class.- It has these changes:
Accepts more initializing values, like hex strings, non-negative integers, and booleans
The representation at console (__repr__) is 0x-prefixed
The to_0x_hex method is added to convert the bytes to a 0x-prefixed hex string
Release Notes
hexbytes v1.2.0 (2024-03-19)
Features
Add
to_0x_hex()
method to provide a quick, explicit way to get an 0x-prefixed string (#43)
hexbytes v1.1.0 (2024-03-01)
Internal Changes - for hexbytes Contributors
hexbytes v1.0.0 (2023-11-07)
Breaking Changes
Internal Changes - for hexbytes Contributors
Added missing build dependency. (#32)
Add
build.os
config for readthedocs (#34)Change to using
pre-commit
to manage linting tools (#35)Merge project template updates and bump mypy to v1.5.1 (#36)
Merge template - .gitignore updates and other fixes (#37)
Merge template updates, including additional linting, move most lint config to pyproject.toml (#39)
HexBytes v0.3.1 (2023-06-08)
Improved Documentation
pull in ethereum-python-project-template updates (#31)
Internal Changes - for hexbytes Contributors
pull in ethereum-python-project-template updates (#31)
HexBytes v0.3.0 (2022-08-17)
Miscellaneous changes
Breaking changes
Drop support for Python 3.6, update Sphinx doc dependency requirement (#27)
HexBytes v0.2.3 (2022-08-11)
Features
Type signature now accepts memoryview when creating HexBytes. It converts to a bytes internally, so not any performance benefit. But at least it’s possible now, and mypy stops complaining. (#22)
Performance improvements
Improve speed of the check for 0x at the beginning of the hex string. Similar to changes in eth-utils. (Technically merged by #22, but first posted in #21) (#21)
Internal Changes - for hexbytes Contributors
Merged in latest changes from project template (#24)
Miscellaneous changes
HexBytes v0.2.2 (2021-08-25)
Miscellaneous changes
Pass mypy tests with –no-implicit-reexport (#15)
Internal Changes - for hexbytes Contributors
Merge in template changes from the last year. Pass pydocstyle tests at the new major version. (#16)
HexBytes v0.2.1 (2020-06-02)
Features
v0.2.0
Released June 3, 2019
v0.1.0
Released Mar 1, 2018
Marked stable
eth-utils v1.0.1 support
v0.1.0-beta.1
Released Feb 21, 2018
pypy3 support
eth-utils v1-beta.2 support
Some generic template updates
v0.1.0-beta.0
Released Jan 30, 2018
Tested a basic integration with eth-rlp
Given the simplicity of the project and the longer usage history in web3.py, it is reasonable to bump to beta immediately.
v0.1.0-alpha.2
Released Jan 30, 2018
Added hypothesis tests
Added some docs
Update eth-utils to get all required functionality
Passes all tests
v0.1.0-alpha.1
Launched repository, claimed names for pip, RTD, github, etc