hexbytes package


Install with:

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 accepts the hex string representation as well, ignoring case and 0x prefixes
>>> hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B')

# get the first byte:
>>> hb[0]

# show how many bytes are in the value
>>> len(hb)

# 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;"


class hexbytes.main.HexBytes

Bases: bytes

HexBytes is a very thin wrapper around the python built-in bytes class. It has these three changes:

  1. Accepts more initializing values, like hex strings, non-negative integers, and booleans
  2. Returns hex with prefix ‘0x’ from HexBytes.hex()
  3. The representation at console is in hex
hex() → str

Just like bytes.hex(), but prepends “0x”