{"id":1505,"library":"hexbytes","title":"HexBytes: Python `bytes` subclass for hex representation","description":"HexBytes is a thin wrapper around the Python built-in `bytes` class (version 1.3.1). It extends `bytes` by accepting a wider range of initialization types including `bool`, `int`, `str`, `bytearray`, and `memoryview`. Key features include a 0x-prefixed console representation (`__repr__`) and a `to_0x_hex()` method for consistent 0x-prefixed hexadecimal string output. The library is actively maintained with regular releases.","status":"active","version":"1.3.1","language":"en","source_language":"en","source_url":"https://github.com/ethereum/hexbytes","tags":["hex","bytes","ethereum","data-conversion","utility"],"install":[{"cmd":"pip install hexbytes","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"HexBytes","correct":"from hexbytes import HexBytes"}],"quickstart":{"code":"from hexbytes import HexBytes\n\n# Convert from bytes to a prettier representation\nhb_from_bytes = HexBytes(b\"\\x03\\x08wf\\xbfh\\xe7\\x86q\\xd1\\xeaCj\\xe0\\x87\\xdat\\xa1'a\\xda\\xc0 \\x01\\x1a\\x9e\\xdd\\xc4\\x90\\x0b\\xf1;\")\nprint(f\"From bytes: {hb_from_bytes}\")\n\n# HexBytes accepts hex string representation (ignoring case and 0x prefixes)\nhb_from_hex_str = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B')\nprint(f\"From hex string: {hb_from_hex_str}\")\n\n# Get the 0x-prefixed hex string\nhex_string_output = hb_from_hex_str.to_0x_hex()\nprint(f\"Using to_0x_hex(): {hex_string_output}\")\n\n# Cast back to basic bytes type\noriginal_bytes = bytes(hb_from_hex_str)\nprint(f\"Cast back to bytes: {original_bytes}\")","lang":"python","description":"Initialize HexBytes from various types and retrieve different representations."},"warnings":[{"fix":"Ensure your Python environment is 3.8 or newer for HexBytes 1.x. For older Python versions, use an older HexBytes release (e.g., <0.3.0 for Python 3.7 compatibility).","message":"HexBytes has dropped support for older Python versions. Versions prior to 0.2.0 dropped Python 3.5, versions prior to 0.3.0 dropped Python 3.6, and current 1.x series (since 1.0.0) requires Python >= 3.8. Attempting to install on unsupported versions will fail or lead to unexpected behavior.","severity":"breaking","affected_versions":"<1.0.0 (Python 3.5, 3.6), >=1.0.0 (Python <3.8)"},{"fix":"To consistently get a 0x-prefixed hex string, always use the `my_hexbytes.to_0x_hex()` method.","message":"While HexBytes' `__repr__` method provides a convenient 0x-prefixed hex string for console output, it does not override the standard `bytes.hex()` or `__str__` methods. Calling `str(my_hexbytes)` or `my_hexbytes.hex()` will revert to the default `bytes` behavior (e.g., `b'\\x01\\x02'` or `'0102'`).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}